[Tex/LaTex] Using \fbox inside \section

boxessectioning

I want to place an \fbox inside of some \part, \chapter, and \section commands. Instead, I get this message:

Use of \@chapter doesn't match its definition.

Update:

\documentclass{article}
\usepackage{xcolor}
\begin{document}
    \section{\fbox{hi}}
\end{document}

Best Answer

Protecting your \fbox in a section command (since it is fragile) allows for it to be used without problem:

enter image description here

\documentclass{article}
\usepackage{xcolor}% http://ctan.org/pkg/xcolor
\begin{document}
\tableofcontents
\section{\protect\fbox{hi}}
\section{\colorbox{green!25}{there}}
\end{document}

Alternatively, the etoolbox package provides \robustify{<cmd>} which can make <cmd> non-fragile:

\documentclass{article}
\usepackage{etoolbox}% http://ctan.org/pkg/etoolbox
\usepackage{xcolor}% http://ctan.org/pkg/xcolor
\robustify{\fbox}% Make \fbox non-fragile
\begin{document}
\tableofcontents
\section{\fbox{hi}}
\section{\colorbox{green!25}{there}}
\end{document}

Similar functionality is provided by makerobust. Also see the discussion What is the difference between Fragile and Robust commands?

Related Question