2016-06-05 7 views
1

Beim Lesen einer Datei csv aktualisiert das LaTeX-Paket csvsimple den Wert einer Variablen, z. B. \location, und identifiziert eine bestimmte Spalte.LaTeX: Bedingte Ausführung der Anweisung bei Änderung einer Variablen

Ich möchte, dass der Wert \location im Text angezeigt wird, nur wenn sein Wert sich tatsächlich ändert.

Das folgende Stück Code funktioniert nicht.

\documentclass{article} 
    \usepackage{csvsimple} 
    \usepackage{ifthen} 
    \begin{document} 

    \def\oldlocation{} 

    \csvreader[head to column names,separator=tab]{input.tab}{}{% 
     \ifthenelse{\equal{\oldlocation}{\location}}{\relax}{% 
      \begin{center}\location{}\end{center} 
      \element{}\\ 
      \renewcommand{\oldlocation}{\location}} 
    } 
    \end{document} 

Probe input.tab:

location[tab]element 
    Shelf 1 [tab] Item A 
    Shelf 1 [tab] Item B 
    Shelf 1 [tab] Item C 
    Shelf 1 [tab] Item D 
    Shelf 1 [tab] Item E 
    Shelf 2 [tab] Item F 
    Shelf 2 [tab] Item G 
    Shelf 2 [tab] Item H 
    Shelf 2 [tab] Item I 
    Shelf 2 [tab] Item J 
    Shelf 3 [tab] Item K 
    Shelf 3 [tab] Item L 
    Shelf 3 [tab] Item M 
    Shelf 3 [tab] Item N 
    Shelf 3 [tab] Item O 

Erwartete Ausgabe:

  Shelf 1 
Item A 
Item B 
Item C 
Item D 
Item E 
      Shelf 2 
Item F 
Item G 
Item H 
Item I 
Item J 
      Shelf 3 
Item K 
Item L 
Item M 
Item N 
Item O 
+0

@Werner: Ja, danke. Bitte, siehe bearbeitete Frage. – ggna

Antwort

1

Das Problem mit dem aktuellen Setup ist das Update von \oldlocation. Mit

\renewcommand{\oldlocation}{\location} 

erweitert nicht \location zu seiner aktuellen Bedeutung. Er setzt nur \oldlocation, um \location abzurufen, wenn er ausgeführt wird. Dies fixiert ist, wenn

\edef\oldlocation{\location} 

Der Code unter Verwendung bietet, was Sie nach mit einigen kleineren Umordnungen des bedingten Code:

enter image description here

\documentclass{article} 

\usepackage{csvsimple} 

\begin{document} 

\def\oldlocation{} 

\csvreader[head to column names,separator=tab]{input.tab}{}{% 
    \expandafter\ifx\expandafter\oldlocation\location\else 
    \begin{center}\location{}\end{center}% 
    \edef\oldlocation{\location} 
    \fi 
    \element{}\\ 
} 

\end{document} 

Sie können auch verwendet werden, haben die folgende e-TeX-Bedingung:

\ifnum\pdfstrcmp{\oldlocation}{\location}=0\else 

die die str vergleicht Erweiterung von \oldlocation mit \location, und Rückgabe von 0 von ihnen sind gleich.


Sie können auch die Verwendung von datatool für diese Art von Setup in Erwägung ziehen.

Verwandte Themen