I am trying to include a first circled and then boxed word in a section heading.

Currently, I am only able to create this highlighted word within a normal paragraph, but not inside of a heading. My attempt so far:

\documentclass[a4paper]{article}
\usepackage{tikz}
\newcommand*\circled[1]{\tikz[baseline=(char.base)]{
\node[shape=circle,draw,inner sep=2pt] (char) {#1};}}
\begin{document}
\fbox{\circled{word}}
\end{document}


Which renders like this:

.

Ideally, I want to be able to write something similar to \fbox{\circled{word}} inside of my section heading, which currently simply throws an error about the "undefined control sequence \def.

• \section{Custom \fbox{\protect\circled{word}} in Section heading} also works... – marmot Dec 4 '18 at 23:40
• @Sermak Are you using hyperref in your real document? – user36296 Dec 7 '18 at 10:25

This is because you command is fragile. Here is a solution:

\documentclass[a4paper]{article}
\usepackage{tikz}
\DeclareRobustCommand\circled[1]{\fbox{\tikz[baseline=(char.base)]{
\node[shape=circle,draw,inner sep=2pt] (char) {#1};}}}

\begin{document}

\circled{word}

\end{document}


Anything inside sectioning commands must be robust or protected using \protect, so you need to change you \newcommand to \DeclareRobustCommand.

\documentclass[a4paper]{article}
\usepackage{tikz}
\DeclareRobustCommand\circled[1]{\tikz[baseline=(char.base)]{
\node[shape=circle,draw,inner sep=2pt] (char) {#1};}}
\begin{document}

• A typical AboAmmar action. You write an answer, I write a comment about \protect, and after you read my comment you just to your answer. We had these conversations before and I am starting to wonder if there is any answer of yours that is really your answer, i.e. not just "adapted" from someone else. – marmot Dec 9 '18 at 10:27
• A typical marmot action: attack others without even thinking. Both answers were posted at 23:36 and your comment at 23:40. Do you think that people are reading your mind and stealing your ideas before you post them? We answer questions here in our free time just for fun and to pay back the community who helped us in the past. This is not a marathon or a war, just take a deep breath and relax. Happy TeX-ing anyway. – AboAmmar Dec 9 '18 at 15:25
• No. Your answer was updated considerable time after my comment, which was on \protect. No, I am not claiming I was the first to point out one may want to use \protect in sections. But you definitely added \protect after my comment was there and before that \protect was not part of your answer. Please stop doing this. Academic honesty is not an unnecessary habit. – marmot Dec 9 '18 at 15:37