3

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:

enter image description here

Warnings:

enter image description here

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

  • 1
    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
  • 1
    Shouldn't \circled be \protected? – GuM Jun 14 '15 at 14:17
  • @GustavoMezzetti: Yes, it should ;-) – user31729 Jun 14 '15 at 14:18
6

\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}

enter image description here

  • 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
  • 1
    @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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.