5

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}
\section{Custom \fbox{word} in Section heading}
\fbox{\circled{word}}
\end{document}

Which renders like this:

rendered output .

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... – user121799 Dec 4 '18 at 23:40
  • @Sermak Are you using hyperref in your real document? – user36296 Dec 7 '18 at 10:25
2

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}

\section{Custom \circled{word} in Section heading}

\circled{word}

\end{document} 

enter image description here

2

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}
\section{Custom \fbox{\circled{word}} in Section heading}
\fbox{\circled{word}}
\end{document}

enter image description here

  • 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. – user121799 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. – user121799 Dec 9 '18 at 15:37

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.