Adding a circle to a section title

I want a circle around a roman numeral inside a section title.

A command \circled is made from:

\documentclass[11pt,a4paper]{article}
\usepackage{tikz}
...

%Circle around characters
\newcommand*\circled[1]{%
\tikz[baseline=(C.base)]\node[draw,circle,inner sep=0.5pt](C) {#1};\!
}


a solution I have from the latex community forum. It works very well surrounding a character with a circle. When I try to put this inside a \subsection command to add it to a section title, it actually works, but I get many errors.

Result:

Warnings:

What is wrong here? It works apparently, but can I fix the errors?

• Please help us help you and add a minimal working example (MWE) that illustrates your problem. Reproducing the problem and finding out what the issue is will be much easier when we see compilable code, starting with \documentclass{...} and ending with \end{document}. Using ... in the preamble is an error – user31729 Jun 14 '15 at 14:12
• Shouldn't \circled be \protected? – GuM Jun 14 '15 at 14:17
• @GustavoMezzetti: Yes, it should ;-) – user31729 Jun 14 '15 at 14:18

\circled is a fragile command this way (due to the \tikz etc content).

It must be \protected then. Or define it with \DeclareRobustCommand, as for example \robustcircled, which can be used without \protect then.

As a general rule: Use robust commands as arguments to the \section etc. commands.

\documentclass[11pt,a4paper]{article}
\usepackage{tikz}

%Circle around characters
\newcommand*\circled[1]{%
\tikz[baseline=(C.base)]\node[draw,circle,inner sep=0.5pt](C) {#1};
}

\DeclareRobustCommand*\robustcircled[1]{%
\tikz[baseline=(C.base)]\node[color=blue,draw,circle,inner sep=0.5pt](C) {#1};
}

\usepackage{pgffor}

\begin{document}

\foreach \x in {1,...,5}{%
\section{\protect\circled{\x} The super theory on brontosaurs}
\foreach \y in {1,...,6}{%
\subsection{\robustcircled{\y} The theory of brontosaurs -- version \y}
}}
\end{document}


• Thank you. In what sense fragile? Does other packages interferre with this homemade command? – Steeven Jun 14 '15 at 14:22
• @Steeven: No, the command breaks into parts and confuses tex. A robust command does not break. – user31729 Jun 14 '15 at 14:23
• @Steeven To add a little to Christian's explanation: fragile commands break when used in moving arguments (such as the arguments for sectional unit commands or \caption, for example). For a comprehensive explanation, see the answers to tex.stackexchange.com/q/4736/3954. – Gonzalo Medina Jun 14 '15 at 17:46
• @GonzaloMedina: Thanks for that further explanation. I was unsure whether I should mention the moving arguments too... – user31729 Jun 14 '15 at 20:04