8

Friends, I've been successfully using the tocloft package to manage custom indices. But now I have some doubts on a tricky part, whether is possible or not. Consider the following code:

\documentclass{article}

\usepackage[titles]{tocloft}
\usepackage{tikz}

\newcommand*\info[1]{%
  \begin{tikzpicture}
    \node[draw,inner sep=1pt, minimum height=0.2cm, minimum width=0.5cm] {\tt\tiny #1};
  \end{tikzpicture}}

\newcommand{\listfoo}{List of foo}
\newlistof[section]{foo}{idf}{\listfoo}

\begin{document}

\info{123} \info{ABC} Hello world.

\refstepcounter{foo}
\addcontentsline{idf}{foo}{Hello world.}

\refstepcounter{foo}
\addcontentsline{idf}{foo}{Hello bar.}

\listoffoo

\end{document}

The output is as expected:

List of foo

In this particular case, I was wondering if it's possible to add a TikZ image to the list entries. I tried to add \addcontentsline{idf}{foo}{\info{123} \info{ABC} Hello world.} but it didn't work. I hope to achieve something like this:

List of foo part 2

I also tried the inline \tikz, but no success. Probably I'm missing something obvious, or maybe it's trickier than I thought. Any ideas?

Paulo Cereda
  • 44,220

2 Answers2

6

Simply protect the \info commands:

\documentclass{article}

\usepackage[titles]{tocloft}
\usepackage{tikz}

\newcommand*\info[1]{%
  \begin{tikzpicture}
    \node[draw,inner sep=1pt, minimum height=0.2cm, minimum width=0.5cm] {\tt\tiny #1};
  \end{tikzpicture}}

\newcommand{\listfoo}{List of foo}
\newlistof[section]{foo}{idf}{\listfoo}

\begin{document}

\info{123} \info{ABC} Hello world.

\refstepcounter{foo}
\addcontentsline{idf}{foo}{\protect\info{123}\ \protect \info{ABC}\ Hello world.}

\refstepcounter{foo}
\addcontentsline{idf}{foo}{\protect\info{123}\ \protect \info{ABC}\ Hello bar.}

\listoffoo

\end{document}
Gonzalo Medina
  • 505,128
3

Define \info with \DeclareRobustCommand instead of \newcommand and say simply

\addcontentsline{idf}{foo}{\info{123} \info{ABC} Hello world.}

You can also use \newrobustcmd from the etoolbox package.

It's common to "robustify" commands that get into "moving arguments" such as what goes in the table of contents.

egreg
  • 1,121,712
  • Hm now I understand why we should "robustify" some commands. =) I'll take a look on the etoolbox package, it seems pretty interesting. Thanks egreg! – Paulo Cereda Jun 18 '11 at 16:48