PDA

Archiv verlassen und diese Seite im Standarddesign anzeigen : [Möglichst LaTeX3]Preis ausgeben



Furantsu Kafuka
13-06-2012, 18:33
Hallo.

Ich versuche gerade eine Funktion zu schreiben, die Euro-Beträge ausgibt, und prüft, ob Euro/Cent 0 bzw. leer gelassen worden sind und dementsprechend die Ausgabe abändert.

(Wie ich 's in C++ machen würde)

#include <iostream>
using namespace std;

void Preis(int Hauptargument, int optionalArgument);
int main()
{
Preis(0,0); // ---
Preis(50,0); // 50,--- €
Preis(100,50); // 100,50 €
Preis(0,99); // 99 Cent
}

void Preis(int Hauptargument, int optionalArgument)
{
if((Hauptargument)&& (Hauptargument>0))// ist Hauptargument belegt und ist es größer als 0 [Pseude-Abfrage, da der Wert in LaTeX auch 'leer' (ungleich 0) sein kann ]
{
if((optionalArgument) && optionalArgument>0) // Cent-Betrag existiert und ist größer als 0
cout << Hauptargument << "," << optionalArgument << " €" << endl;
else // Cent-Betrag existiert nicht oder ist kleiner als 1
cout << Hauptargument << ",--- €" << endl;
}
else
{
if((optionalArgument) && optionalArgument>0) // Cent-Betrag existiert und ist größer als 0
cout << optionalArgument << " Cent" << endl;
else // Weder Euro noch Cent sind gültig
cout << "---" << endl;
}
}


Und hier meine LaTeX-Implementation (unkompilierbar, wegen \Preis)

%---------------------------------------------------------------------------
\documentclass%%
%---------------------------------------------------------------------------
[fontsize=11pt,%% Schriftgroesse
%---------------------------------------------------------------------------
% Satzspiegel
paper=a4,
%---------------------------------------------------------------------------
% Formatierung
draft=off%% Entwurfsmodus
]{scrlttr2}

\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\DeclareUnicodeCharacter{20AC}{\texteuro}
\usepackage{xparse,l3int}

\newcommand{\Zahl}[1]{\textsc{%\fontfamily{lmssq}\selectfont
#1}%\fontfamily{iwonalc}\selectfont
}

\DeclareDocumentCommand{\Preis}{m O{\NoValue}} {%
\newcount\ArgA
\newcount\ArgB
\ArgA=#1
\ArgB=#2
% Cent wurden NICHT übergeben
\IfNoValueTF {#2}
{
% auch keine Euro-Angabe
\IfNoValueTF {#1}
{
---
}{% vielleicht eine Euro-Angabe
\ifnum\value{\ArgA}<1 % Null oder kleiner, ergo ungültig!
---
\else
\Zahl{#1},--- € % ja, es gibt eine Euro-Angabe
\fi
}
}% Cent wurden übergeben
{
% Euro-Angabe ist leer
\IfNoValueTF {#1}%
{%
\ifnum\value{\ArgB}<1 % Cent-Betrag beträgt null oder kleiner -- ungültig!
---
\else% Immerhin ist der Cent-Betrag gültig
\Zahl{#2} Cent
\fi
}% Euro-Angabe ist nicht leer
{
\ifnum\value{\ArgA}<1 % Euro-Betrag ist null oder kleiner, also Cents ausgeben.
\Zahl{#2} Cent
\else%
\Zahl{#1},\Zahl{#2} €
\fi
}
}
}

%\DeclareDocumentCommand{\Preis}{m O{\NoValue}} {%
\newenvironment{Kosten}{%
\begin{tabular}{lll}\toprule
\bfseries Artikel &\bfseries Anzahl &\bfseries Kosten pro Stk. \\
\midrule}{\end{tabular}}

\newcommand{\KostenEintrag}[4]{\enquote{#1}&\Zahl{#2}&\Preis{#3}[#4]\\}

\begin{document}
\begin{Kosten}
\KostenEintrag{Die unendliche Geschichte}{1}{20}{50}
\KostenEintrag{Momo}{1}{10}{}
\end{Kosten}
\end{document}

mechanicus
14-06-2012, 18:01
Hi,

anbei ein Vorschlag.


\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage{textcomp}
\DeclareUnicodeCharacter{20AC}{\texteuro}

\usepackage{xparse,expl3}
\ExplSyntaxOn
\NewDocumentCommand \Zahl { m } { \textsc{ #1 } }
\NewDocumentCommand \KeinGeld { } { --- }

\tl_new:N \l_preis_euro_tl
\tl_new:N \l_preis_cent_tl

\DeclareDocumentCommand \Preis { m O{} }
{
\tl_set:Nn \l_preis_euro_tl { #1 }
\tl_set:Nn \l_preis_cent_tl { #2 }
\tl_if_empty:NT \l_preis_euro_tl { \tl_set:Nn \l_preis_euro_tl { 0 } }
\tl_if_empty:NT \l_preis_cent_tl { \tl_set:Nn \l_preis_cent_tl { 0 } }
\int_compare:nTF { \l_preis_euro_tl <= \c_zero }
{
\preis_cent_ohne_euro:
}
{
\preis_cent_mit_euro:
}
}

\cs_new:Npn \preis_cent_ohne_euro:
{
\int_compare:nTF { \l_preis_cent_tl <= \c_zero }
{ \KeinGeld }
{ \Zahl{ \l_preis_cent_tl }\,Cent }
}

\cs_new:Npn \preis_cent_mit_euro:
{
\int_compare:nTF { \l_preis_cent_tl <= \c_zero }
{ \Zahl{ \l_preis_euro_tl },---\,€ }
{ \Zahl{ \l_preis_euro_tl },\Zahl{ \l_preis_cent_tl }\,€ }
}
\ExplSyntaxOff

\begin{document}
\Preis{-2}[25]

\Preis{2}[25]

\Preis{2}

\Preis{}[]
\end{document}

Allerdings würde ich eine Eingabe von 22,15 oder 22 oder 0,25 vermutlich als leichter empfinden. Die Abarbeitung ist nicht schwieriger

PS: LaTeX3 ist die falsche Wortwahl. expl3 ist die Basissprache von LaTeX3 und xparse bildet EINE Nutzerschnittstelle zur Basis.

Marco

Furantsu Kafuka
15-06-2012, 20:28
Danke für deine Antwort, aber das Beispiel ist (auch) nicht kompilierbar.

LaTeX Font Info: ... okay on input line 45.
! Undefined control sequence.
\\Preis ... \l_preis_cent_tl {0}}\int_compare:nTF
{\l_preis_euro_tl <=\c_zer...
l.46 \Preis{2}[25]
The control sequence at the end of the top line
of your error message was never \def'ed. If you have
misspelled it (e.g., `\hobx'), type `I' and the correct
spelling (e.g., `I\hbox'). Otherwise just continue,
and I'll forget about whatever was undefined.

mechanicus
15-06-2012, 21:44
Hi,

dann hast du noch eine alte Version von expl3.

Marco