0

Intro

While trying to get my head a round my current problem (To add a line to the toc, depending on the comparison of the values of two makros/commands: please see here: Add tocline depending on string compare) I found out, the problem might have more to do with the redefinition of a command, than with the actual logic of strcmp or some other thing.

So here is my attempt to give a structured current state of my files and problem.

Short summary

Ultimately what I want to do is:

  • Include multiple files in a root-file (the files are named KW\i.tex - with \i being a number increased after a file is included)
  • Every file shall renew the value of \Field which holds a string like Dummyfield with the line \renewcommand{\Field}{My Field}
  • Before this override happens I want to save the last file's \Field in \OldField. Currently I'm using \let\OldField\Field as Werner suggested
  • The initial definition of the commands are

    \newcommand{\Field}{Dummyfield}
    
    \newcommand{\OldField}{}
    
  • Compare the values of \Field and \OldField (with pdfstrcmp). If they are different, aka the field changes, add a new entry to the toc. Especially if they are equal, a second identical entry must not be written to the toc

Root-file

% Dokument-Grundeinstellungen
\documentclass[11pt, a4paper, titlepage, parskip=half]{scrbook}
%   \usepackage{tocbasic}
\usepackage[a4paper,lmargin=2cm,rmargin=2cm,tmargin=2.5cm,bmargin=2.5cm]{geometry}  % Format und Seitenraender
\usepackage[onehalfspacing]{setspace}                                                   % Zeilenabstand
%------------------------------------------------------------

% Schriftart
%\usepackage[scaled=.92]{helvet}                    % Helvetica (skaliert) als serifenlose Schriftart (Standard fuer serifenlose Schrift)  -- besser geeigent zum Lesen am BILDSCHIRM
    % \renewcommand{\familydefault}{\sfdefault}     % (Eingestellte) serifenlose Schrift als Standardschrftart (fuer Fliesstext)
\usepackage{lmodern}                                % Besser lesbare Standardschriftart (Serifenschrift) -- besser geeigent zum Lesen auf PAPIER
\usepackage{courier}    % Schritart Courier als Schrift einstellen
    \renewcommand{\familydefault}{\rmdefault}       % (Eingestellte) Serifenschriftart als Standardschrift (fuer Fliesstext)
    \setkomafont{sectioning}{\rmfamily\bfseries}    % Ueberschriften in Serifenschrift (fett)
    %\setkomafont{sectioning}{\rmfamily}            % Ueberschriften in Serifenschrift (plain)
\usepackage{xcolor}                                 % Textfarbe aendern - Fuer Dokument, Paragraph oder diskrete Texteile mit '\textcolor{Farbe}{Text}'



%------------------------------------------------------------

% Kopf- und Fusszeile
\usepackage[markcase=noupper, headsepline=false]{scrlayer-scrpage}  % no uppercasing
        \setkomafont{pageheadfoot}{\sffamily}
        \setlength{\headheight}{2cm}
        \setlength{\footheight}{2cm}
        \newcommand*{\Field}{Dummyfield}
        \newcommand*{\OldField}{}
            %\let\OldField\Field
    \ihead{Name: Jon Doe\\ Field: \Field}
    %\cfoot{\pagemark}

%------------------------------------------------------------

% Ausgabe von Umlauten (vor 'inputenc' einbinden!)
\usepackage[T1]{fontenc}        % Schoene Ausgabe von Umlauten
%------------------------------------------------------------

% Deutsche Eingabe
\usepackage[utf8]{inputenc}     % Umlaute direkt eingeben   UTF-8 zur besseren Zusammenarbeit mit vers. Betriebssystemen
\usepackage[ngerman]{babel}     % deutsche Eingabe (Zur Eingabe von Umlauten wird 'inputenc' benötigt)
%------------------------------------------------------------

% Mathematik-Bibliotheken
\usepackage{amsmath}    % Schoene Formelumgebung - align{, \[ \], ... - NICHT eqnarray, displaymath 
\usepackage{amssymb}    % mathematische Schriftarten, Zeichensaetze und Symbole - laedt automatisch das Paket 'asmfonts'
\usepackage{mathptmx}   % Funktion aktuell Unklar. Vmt: Verbindung von drei Schriftarten fuer serifen, serifenlosund typewriter Fonts: Times/Helvetica/Courier (Helvetica akaliert - siehe Schriftart)
\usepackage[exponent-product = \cdot, decimalsymbol=comma, per=frac ]{siunitx}  % SI-Einheiten verwenden Bsp: 
\usepackage{datetime}
    \renewcommand{\dateseparator}{.}
\usepackage{advdate}                                    % Datumberechnungen anstellen
    \newcommand{\Datum}{00/00/0000}     % Leeres Datum vorbelegen
\usepackage{ifthen}
%------------------------------------------------------------

% Weitere Zeichensaetze
\usepackage{textcomp}   % Erweiterter Zeichensatz
\usepackage{enumitem}   % spez. Nummerierungen z.B. a) b) c)
\usepackage[autostyle=true, german=quotes]{csquotes}
%------------------------------------------------------------

% Bilder, Grafiken und Dokumente einbinden
\usepackage{graphicx}   % Bilder und Grafiken einbinden '\includegraphics[Optionen]{Herkunft/Speicherort}'
\usepackage{here}       % Figures (Bilder) genau an dieser Stelle einbinden / Einbinden erzwingen - Option [H] - !!ACHTUNG!! Als Option fuer Gleitumgebungen gedacht, widerspricht aber dem Sinn einer Gleitumgebung !
\usepackage{pdfpages}   % PDF-Dokumente einbinden
\usepackage{adjustbox}  % Objektgroessen anpassen/skalieren
\usepackage{tabularx}
\usepackage{array}
    \newcolumntype{P}[1]{>{\centering\arraybackslash}p{#1}}
\usepackage{colortbl}
\usepackage{multirow}
\usepackage{pgf}
    \usepackage{pgfplots}
        \pgfplotsset{compat=1.16}
    \usepackage{pgffor}     
\usepackage{tikz}
\usepackage[absolute, overlay]{textpos}
    \setlength{\TPHorizModule}{1mm}
    \setlength{\TPVertModule}{1mm}
\usepackage{pdftexcmds}
%------------------------------------------------------------

% Referenzen/Links lauffaehig machen
\usepackage[pdftex]{hyperref}       % Unmittelbares Erzeugen von PDF-Ausgabe / Verknuefpungen/Links herstellen UND Ermoeglichung von urls: \url{}
\usepackage{cleveref}               % Erweiterte Refernzenbefehle -- IMMER NACH hyperref lade
    \crefname{enumi}{Punkt}{Punkte} % Aendert den angezeigten Namen des Labels 'enumi' in 'Punkt' bzw. 'Punkte' (Plural)
    \crefname{figure}{Abbildung}{Abbildungen}
    \crefname{eqation}{Gleichung}{Gleichungen}
    \Crefname{equation}{Gleichung}{Gleichungen}
    \crefname{subsection}{Unterabschnitt}{Unterabschnitte}
    %\Crefname{subsection}{Unterabschnitt}{Unterabschnitte}
\usepackage[version=4]{mhchem}      % Noeatig um cref-Namen neu zu besetzen
\renewcommand{\listfigurename}{Abbildungen}


% Dauer-Kommando 
\newcommand{\dauer}[1]{\SI{#1}{\hour}}
% Kalendarwochen-Kommando
\newcommand{\KW}{00} 



\begin{document}
 %------------------------------------------------------------

\tableofcontents                % Inhaltverzeichnis erzeugen
\newpage
%-----------------------------------------------------------
    %\cleardoublepage
    %\cleardoublepage   
    \include{./2018/2018}
    \foreach \i in {00, 01, 02, ..., 99}{%
        \edef\FileName{./2018/Wochen/KW\i}%
        \IfFileExists{\FileName}{%
        \include{\FileName}%
        }
    }
\end{document}

Data-files (KW\i.tex)

% ----------------------------------------EINSTELLUNGEN--------------------------------------------------
% ---------------------------- ANFANGSTAG  UND KW SETZEN ----------------------------------------
                                    \renewcommand{\Datum}{03/09/2018}                           % <--- Datumsvariable neu setzen {TT/MM/JJJJ}
                                    \renewcommand{\KW}{36}                                                  % <--- KW neu setzen; z.B. {36} fuer KW 136
% --------------------------------------------------------------------------------------------------------------
\SetDate[\Datum]%---------------------------------------------------------------------------------------      % \today per advdate aendern
% --------------------------------------------------------------------------------------------------------------
%\show\OldField
\let\OldField\Field% ------------------- Vorherigen Bereich speichern ----------------------
%\expandafter \renewcommand\expandafter\OldField\expandafter{\Field}
%\show\OldField
% ------------------------------------ BEREICH SETZEN ---------------------------------------------------
                            \renewcommand{\Field}{My Field}                                 % <--- Neuen Bereich setzen                 
% --------------------------------------------------------------------------------------------------------------
%\show\OldField 
% --------------------------------------------------------------------------------------------------------------
\section*{KW \KW{} \hspace{12pt}\today{} - \AdvanceDate[4]\today}%----------------
\ifnum\pdfstrcmp{\Field}{\OldField}=0
\else
    \addtocounter{section}{1}
    \addcontentsline{toc}{section}{\Field}%------------------------------------------------------
\fi


%\ifthenelse{ \not\equal{\OldField}{\Field}  }{%---------------------------------------------------
%   \addtocounter{section}{1}%------------------------------------------------------------------------
%   \addcontentsline{toc}{section}{\Field}}{%------------------------------------------------------
%}%-------------------------------------------------------------------------------------------------------------
%  -------------------------------------------------------------------------------------------------------------
\SetDate[\Datum] %-- Datum zurücksetzen auf Wochenanfang-----------------------------
%--------------------------------------------------------------------------------------------------------------

Currently there are two files KW36.tex and KW37.tex. They both have the same field \renewcommand{\Field}{My Field}

I checked the .log-File to see the values of \Field and \OldField at the three points you can see in the Data-Files. This table holds the corresponding values:

Values from the two files at the three specified points in the code

The Question(s)

This behavior begs the simple question: WHY? :D After file KW37.tex renewed the command with \renewcommand{\OldField}{\Field}, shouldn't \Field and \OldField hold the same value My Field?

3
  • Welcome to TeX.SX! Any chance of cutting this down to a minimal working example. Without wading through you code I wonder if you are setting \Field inside a group.
    – user30471
    Oct 10, 2018 at 14:42
  • 1
    To add insult to injury your code doesn't produce the table above at all! In future posts can you please post a minimal working example that compiles, if possible, and is as small as possible to reproduce your problem and what you describe in the question.
    – user30471
    Oct 10, 2018 at 14:51
  • You're right. Next time a try to give a slimmer example. To my defence, I never told the code would produce this particular table. The table is "handmade" to see the values of \Field and \OldField at different steps in the code ;) Oct 10, 2018 at 14:55

1 Answer 1

1

\foreach executes its contents inside a group, so local definitions are unknown between iterations.

You should make the definitions global by prefixing \global to \let and using \gdef:

% ---------------------------- ANFANGSTAG  UND KW SETZEN ----------------------------------------
                                    \renewcommand{\Datum}{03/09/2018}                           % <--- Datumsvariable neu setzen {TT/MM/JJJJ}
                                    \renewcommand{\KW}{36}                                                  % <--- KW neu setzen; z.B. {36} fuer KW 136
% --------------------------------------------------------------------------------------------------------------
\SetDate[\Datum]%---------------------------------------------------------------------------------------      % \today per advdate aendern
% --------------------------------------------------------------------------------------------------------------
%\show\OldField
\global\let\OldField\Field% ------------------- Vorherigen Bereich speichern ----------------------
%\expandafter \renewcommand\expandafter\OldField\expandafter{\Field}
%\show\OldField
% ------------------------------------ BEREICH SETZEN ---------------------------------------------------
                            \gdef\Field{My Field}                                 % <--- Neuen Bereich setzen                 
% --------------------------------------------------------------------------------------------------------------
%\show\OldField 
% --------------------------------------------------------------------------------------------------------------
\section*{KW \KW{} \hspace{12pt}\today{} - \AdvanceDate[4]\today}%----------------
\ifnum\pdfstrcmp{\Field}{\OldField}=0
\else
    \addtocounter{section}{1}
    \addcontentsline{toc}{section}{\Field}%------------------------------------------------------
\fi


%\ifthenelse{ \not\equal{\OldField}{\Field}  }{%---------------------------------------------------
%   \addtocounter{section}{1}%------------------------------------------------------------------------
%   \addcontentsline{toc}{section}{\Field}}{%------------------------------------------------------
%}%-------------------------------------------------------------------------------------------------------------
%  -------------------------------------------------------------------------------------------------------------
\SetDate[\Datum] %-- Datum zurücksetzen auf Wochenanfang-----------------------------
%--------------------------------------------------------------------------------------------------------------

I just replaced \let\OldField\Field by \global\let\OldField\Field and \renewcommand{\Field}{My Field} by \gdef\Field{My Field}.

1
  • Thank You! Using \global and \gdef fixed the problem. Oct 10, 2018 at 16:43

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .