TransWikia.com

How can I use macros in addcontentsline?

TeX - LaTeX Asked by holconius on August 18, 2021

A word of warning to begin with: I doubt I have fully grasped def, edef, let or expandafter and feel utterly stumped after a long string of attempts to solve the following problem.

I would like to add custom labels to section titles, i.e. there are different parts to my document and title strings should reflect that. Section titles should be preceded by A1, A2, A3, … K1, K2, K3, … D1, D2, D3, … depending on the counter that is supplied to a macro unit{counter}{title}.

Creating counters is no problem, conditionals work and adding a starred section command to suppress the default section numbering is straightforward. All hell breaks loose when addcontentsline comes into play, telling me that endcsname has been inserted (In some variations of def and expandafter in my code I ended up with an error saying that the very section macro I had defined was undefined — although the hyperref package seems to have caused some of the issues there). Be that as it may, below is a minimal example.

documentclass{article}

usepackage{ifthen}

newcounter{parta}
setcounter{parta}{0}
newcounter{partb}
setcounter{partb}{0}

newcommandunit[2]{%
  deftag{A}
  defcnt{#1}
  ifthenelse{equal{#1}{morph}}%
    {deftag{A}}%
    {%
      ifthenelse{equal{#1}{syntax}}%
      {deftag{B}}%
      {}%
    }%
  defnumtag{refstepcounter{cnt}tagarabic{cnt}}
  expandafterdefexpandafterunittitle{numtag{} #2}
  section*{unittitle}
  addcontentsline{toc}{section}{unittitle}}

begin{document}
unit{parta}{foo}
unit{partb}{bar}
end{document}

The error I receive is

! Missing endcsname inserted.
<to be read again> 
                   csnameendcsname 
l.27 unit{parta}{foo}

I am aware I could probably achieve something like this using the titlesec package or similar, but the process seemed simple enough at the outset. I assume this can be easily resolved and I am being stupid. Aren’t I just adapting strings that are expanded before being shipped out to auxiliary files? Thanks for your help!

2 Answers

Not sure if this is what you are looking for.

documentclass{article}

usepackage{ifthen}
newcounter{parta}
newcounter{partb}
newcounter{partc}

newcommandunit[2]{%
  defmytag{A}
  setcounter{#1}{value{#1}}
  ifthenelse{equal{#1}{partc}}%
    {defmytag{C}}%
    {%
      ifthenelse{equal{#1}{partb}}%
      {defmytag{B}}%
      {}%
    }%
  refstepcounter{#1}%
  edefnumtag{mytagarabic{#1}}
  expandafterdefexpandafterunittitleexpandafter{numtag{} #2}
  section*{unittitle}
  addcontentsline{toc}{section}{unittitle}%
}

begin{document}
tableofcontents
hrulefill

unit{parta}{foo}
unit{parta}{foo-ex}
unit{partb}{bar}
unit{partb}{bar none}
unit{partb}{barbel}
unit{partc}{foo-bar}
end{document}

enter image description here

Answered by Steven B. Segletes on August 18, 2021

You're making things a bit too complicated. There's no need to condition on whatever you're passing as you can define "prefixes" to these numbers/counter in the following way:

enter image description here

documentclass{article}

newcommand{parta}{A}% Prefix for parta
newcommand{partb}{B}% Prefix for partb
newcounter{parta}
newcounter{partb}

newcommandunit[2]{%
  stepcounter{#1}% Step counter
  section*{csname #1endcsnamearabic{#1} #2}% Section title is "<counter prefix><counter> <title>"
  addcontentsline{toc}{section}{csname #1endcsnamearabic{#1} #2}% Add to ToC
}

begin{document}

tableofcontents

unit{parta}{foo}
unit{partb}{bar}
unit{parta}{baz}

end{document}

The above works because content written to the ToC is expanded, fully, and can be since the definition passed to is very simple. In your example - unittitle - was comprised of numtag, which included a refstepcounter - a macro that does a lot of background calculations and definitions.


In fact, LaTeX provides a fairly easy way to handle what you're after. There is a difference between the value of a counter and it's representation. The value is something internal to LaTeX, while the representation could be roman, or arabic, or alphabetic. So, you could add to this representation a prefix by redefining the<counter>:

documentclass{article}

newcounter{parta}
newcounter{partb}
renewcommand{theparta}{Aarabic{parta}}
renewcommand{thepartb}{Barabic{partb}}

newcommandunit[2]{%
  stepcounter{#1}% Step counter
  section*{csname the#1endcsname{} #2}% Section title is "<counter prefix><counter> <title>"
  addcontentsline{toc}{section}{csname the#1endcsname{} #2}% Add to ToC
}

begin{document}

tableofcontents

unit{parta}{foo}
unit{partb}{bar}
unit{parta}{baz}

end{document}

Answered by Werner on August 18, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP