5

I need to draw a simple symbol with tikz/pgf to a chapter command, for example:

\documentclass{book}
\usepackage{tikz}

\begin{document}

\chapter{hello tikz\tikz\node{pgf};}

\end{document}

but this does not work, is there anyway to fix this? thanks.

lockstep
  • 250,273
ocean
  • 981

3 Answers3

10

Anything inside sectioning commands must be robust or protected using \protect. If you are using hyperref (which I assume) you need also take special care because the content will then end up as PDF bookmarks which don't support anything expect normal text.

I would suggest using the following code:

\documentclass{book}
\usepackage{tikz}
\usepackage{hyperref}

\DeclareRobustCommand{\TikzStuff}{\texorpdfstring{\tikz\node{pgf};}{replacement text}}

\begin{document}
\chapter{hello \TikzStuff}
\end{document}

Instead of the older \DeclareRobustCommand (which doesn't produce an error if the command already exists) the \newrobustcmd command from etoolbox can be used which takes advantage of the e-TeX extension of LaTeX. This can also be used directly using \protected\def\TikzStuff{..}.

Martin Scharrer
  • 262,582
3

I believe that your porblem is similar to Undefined color error, if have symbol in Chapter Title. Try this:

\documentclass{book}
\usepackage{tikz}

\DeclareRobustCommand{\MyTikzNode}{\tikz\tikz\node{pgf};}

\begin{document}
\chapter{hello \MyTikzNode}
\end{document}
Peter Grill
  • 223,288
1

I stumbled upon this post while trying to do something similar; I ended up implementing the following new commands, which, though naive, worked smoothly enough for my document:

\documentclass{amsart}

\newcommand{\ellipse}[1]
{
     \begin{center} \tikz\node[draw, fill = blue!20!white, ellipse]{#1}; \end{center}
}
\newcommand{\hide}[1]
{
    \texorpdfstring{\protect\phantom{\hspace*{5em}#1}}{#1}
}
\newcommand{\csec}[1]
{
    \section{\hide{#1}} 
    \vspace*{-5ex}\ellipse{#1}
}

\usepackage{amsfonts, amsmath, amssymb, amsthm}
\usepackage{xcolor}
\usepackage{tikz}
\usetikzlibrary{shapes, snakes}
\usepackage{hyperref}

\begin{document}
\section*{Note}
To make a circled, unnumbered section, you could define a new command \verb`csecun` just like \verb`csec` but using the \verb`section*` command.
\csec{First Section}
\csec{Second Section}
\end{document}
TJJ
  • 111