# How to add a tikz/pgf command to section command

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.

-

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{..}.

-

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}

-

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 \verbcsecun just like \verbcsec but using the \verbsection* command.
\csec{First Section}
\csec{Second Section}
\end{document}

-
Welcome to TeX.sx! –  texenthusiast Apr 5 '13 at 18:47