E.g., \addcontentsline{toc}{section}{Foo}
beneath other things triggers that during the inputting of the .toc-file the macro \contentsline
does
\csname l@section\endcsname
→ \l@section
.
Thus the second argument of \addcontentsline
must consist of tokens which can be used between \csname..\endcsname
, i.e., tokens whose expansion does not yield non-character-tokens.
\ifthenelse
-stuff yields non-character-tokens and therefore can be used neither between \csname..\endcsname
nor with macro-arguments that at some stage are inserted between \csname..\endcsname
. Same for macros whose expansion at some stage yields \ifthenelse
-stuff.
If you insist in sticking to \ifthenelse
-stuff you need to change the order of processing to have \ifthenelse
-stuff done before it comes to processing \addcontentsline
—what about this? :
\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage{xifthen} % If else commands
\makeatletter
\newcounter{counter@section@level}%
% The optional argument of \DoWithCurrentSectionString denotes tokens that
% process "{section}" as argument.
\newcommand{\DoWithCurrentSectionString}[1][\@firstofone]{%
\ifthenelse{\equal{\value{counter@section@level}}{0}}{#1{section}}{%
% \ifthenelse{\equal{\value{counter@section@level}}{1}}{#1{subsection}}{%
% ...
% }%
}%
}%
\makeatother
\begin{document}
\tableofcontents
\noindent\hrulefill
\DoWithCurrentSectionString % to check the macro itself, works as expected
\kern\ht\strutbox\hrule\hfill
\section*{Foo}
\addcontentsline{toc}{section}{Foo} % works as expected
\section*{Bar}
\DoWithCurrentSectionString[{\addcontentsline{toc}}]{Bar} % does work
\section{FooBar} % overlapping in toc
\end{document}
the .aux-file:
\relax
\@writefile{toc}{\contentsline {section}{Foo}{1}{}\protected@file@percent }
\@writefile{toc}{\contentsline {section}{Bar}{1}{}\protected@file@percent }
\@writefile{toc}{\contentsline {section}{\numberline {1}FooBar}{1}{}\protected@file@percent }
\gdef \@abspage@last{1}
the .toc-file:
\contentsline {section}{Foo}{1}{}%
\contentsline {section}{Bar}{1}{}%
\contentsline {section}{\numberline {1}FooBar}{1}{}%
For the sake of having fun here is an expandable approach which does with delimited arguments instead of \if...
-constructs:
\documentclass{article}
\usepackage[utf8]{inputenc}
\makeatletter
\newcounter{counter@section@level}
\newcommand{\currentSectionString}{%
\expandafter\currentSectionStringSelect\expandafter{\number\value{counter@section@level}}%
}%
\newcommand\currentSectionStringSelect[1]{%
\currentSectionStringFork
#1!1!2!3!4!{section}%
0!#1!2!3!4!{subsection}%
0!1!#1!3!4!{subsubsection}%
0!1!2!#1!4!{paragraph}%
0!1!2!3!#1!{subparagraph}%
0!1!2!3!4!{subparagraph}%
!!!!%
}%
\@ifdefinable\currentSectionStringFork{%
\long\def\currentSectionStringFork#10!1!2!3!4!#2#3!!!!{#2}%
}%
\makeatother
\begin{document}
\tableofcontents
\noindent\hrulefill
\currentSectionString % to check the macro itself, works as expected
\kern\ht\strutbox\hrule\hfill
\section*{Foo}
\addcontentsline{toc}{section}{Foo} % works as expected
\section*{Bar}
\addcontentsline{toc}{\currentSectionString}{Bar} % does work
\section{FooBar} % overlapping in toc
\end{document}
the .aux-file:
\relax
\@writefile{toc}{\contentsline {section}{Foo}{1}{}\protected@file@percent }
\@writefile{toc}{\contentsline {section}{Bar}{1}{}\protected@file@percent }
\@writefile{toc}{\contentsline {section}{\numberline {1}FooBar}{1}{}\protected@file@percent }
\gdef \@abspage@last{1}
the .toc-file:
\contentsline {section}{Foo}{1}{}%
\contentsline {section}{Bar}{1}{}%
\contentsline {section}{\numberline {1}FooBar}{1}{}%