1

How can I compare the content of \etocname to a string? For example to choose the color based on the section name.

\documentclass{article}
\usepackage{xcolor}
\usepackage{xstring}
\usepackage{etoc}

\etocsetstyle{section}
    {}
    {}
    {\IfStrEq{bla}{\etocname}{\color{blue}\etocname}{\color{red}\etocname}}
    {}

\begin{document}
\tableofcontents
\section{bla}
\section{blub}
\end{document}
guest
  • 11
  • It's a subtle point; can you please show a more realistic use case? – egreg Jun 12 '17 at 13:24
  • @egreg Thanks for your reply! I recently saw this question: https://tex.stackexchange.com/questions/374319/customise-table-of-contents and wanted to do something similar and select the color based on three possible names of the sections. If I can test for the name, I can easily choose the colour. – guest Jun 12 '17 at 13:39
  • 1
    You may try \etocthelinkedname instead of \etocname, but be careful that \IfStrEq doesn't work with several commands (say \textbf or accents) in its arguments. – egreg Jun 12 '17 at 13:45
  • @egreg Great! That works! – guest Jun 12 '17 at 13:47
  • @egreg Sounds like your comment solved the problem. Would you like to write an answer? – samcarter_is_at_topanswers.xyz Jun 12 '17 at 21:29
  • @samcarter No, the example in the question does nothing sensible and some more is needed to test it. – egreg Jun 12 '17 at 21:39
  • @egreg \etocthename is perhaps better: \etocthelinkedname is a wrapper including hyperlinking hyperref stuff. –  Jun 19 '17 at 21:29
  • @jfbu Feel free to add an answer, if you can make a sense out of the OP's code. – egreg Jun 19 '17 at 21:35

1 Answers1

1

Expanding on a comment by @egreg, it is simpler to use \etocthename. It contains simply as a macro the section name as present in the .toc file. Of course you can have formatting macros therein. The \etocname is a robust macro which additionally contains hyperref stuff. The \etocthename expands in one step to the name from the .toc file (which may very well contain boxes, \color, \textbf or whatever..)

update:

The OP's code amended to use \etocthename. Also, I have added \noindent and \par, as this probably what will be used anyhow.

\documentclass{article}
\usepackage{xcolor}
\usepackage{xstring}
\usepackage{etoc}

\etocsetstyle{section}
    {}
    {\noindent}
    {\IfStrEq{bla}{\etocthename}{\color{blue}\etocname}{\color{red}\etocname}\par}
    {}

\begin{document}
\tableofcontents
\section{bla}
\section{blub}
\end{document}

enter image description here

note: the \tableofcontents is always typeset by etoc inside a group, which limits the scope of the \color command, but in real application here you may want to add braces to limit more locally the use of the \color (for example if page numbers are incorporated to the TOC via \etocpage, to not contaminate them.)

note: testing \etocthenumber rather than \etocthename could be easier.