11

I am trying to insert an icon (a pdf file) literally next to the section name.

enter image description here

I want the little shield icon to be where the <Icon> is.

The code to generate this image is:

\subsection{Sentinel $<$Icon$>$}\icon{static/sentinel.pdf}

If I try to add the \icon inside the \subsection:

\subsection{Sentinel \icon{static/sentinel.pdf}}

I get an error.

! Missing \endcsname inserted.
<to be read again>
               \csname\endcsname 
l.42 ...ction{Sentinel \icon{static/sentinel.pdf}}                                                  
? 
! Emergency stop.
<to be read again> 
                   \csname\endcsname 
l.42 ...ction{Sentinel \icon{static/sentinel.pdf}}
!  ==> Fatal error occurred, no output PDF file produced!
Transcript written on generated//manual.log.
make: *** [all] Error 1

If possible it would be nice that the icon doesn't appear in the table of content and that the link from the table of content continues to work.

\icon is defined by

\newcommand{\icon}[1]{\begingroup
\setbox0=\hbox{\includegraphics[height=12pt,keepaspectratio]{#1}}%
\parbox{\wd0}{\box0}\endgroup}
3
  • Welcome to TeX.SX! I suspect \protect\icon{static/sentinel.pdf} should work. However, you will get into troubles with bookmarks, if hyperref is used
    – user31729
    Apr 19, 2015 at 1:28
  • Yeah it does, even in the table of content with hyperref, but I'd prefer if there was a way that it wouldn't appear at all in the table of content.
    – user76557
    Apr 19, 2015 at 1:34
  • Use \subsection[title for the toc]{\protect\icon{....}} then. The sectioning commands (at least of standard classes) all have an optional first argument for providing a shorter title (or optional title) which enters the toc and the mandatory argument for the really header of the chapter, section etc. My concern was rather the complaints by hyperref about non-allowed tokens in the PDFString issue. There should be a \texorpdfstring{\protect\icon{}}{} command.
    – user31729
    Apr 19, 2015 at 1:35

1 Answer 1

15

You need some \protection. BTW, that complicated \newcommand can be simplified.

\documentclass{article}
\usepackage{graphicx}
\newcommand{\icon}[1]{\includegraphics[height=12pt]{#1}}
\begin{document}
  \section{Some section here}
  \subsection{Sentinel \protect\icon{example-image-a}}
\end{document}

If you don't want to \protect at many instances, you have these possibilities. First make it robust using etoolbox.

\documentclass{article}
\usepackage{graphicx}
\usepackage{etoolbox}
\newcommand{\icon}[1]{\includegraphics[height=12pt]{#1}}
\robustify{\icon}
\begin{document}
  \section{Some section here}
  \subsection{Sentinel \icon{example-image-a}}
\end{document}

Or use \newrobustcmd (from etoolbox again) like

\usepackage{etoolbox}
\newrobustcmd{\icon}[1]{\includegraphics[height=12pt]{#1}}

Or use an optional argument like

\subsection[<opt argument>]{Sentinel \icon{example-image-a}}

etc....

If you are using hyperref you may need to use \texorpdfstring{\icon{example-image-a}}{} to get rid of the warning and the aftermath.

\documentclass{article}
\usepackage{graphicx}
\usepackage{etoolbox}
\usepackage{hyperref}
\newrobustcmd{\icon}[1]{\includegraphics[height=12pt]{#1}}
\begin{document}
  \section{Some section here}
  \subsection{Sentinel \texorpdfstring{\icon{example-image-a}}{}}
\end{document}

enter image description here

3
  • Wow that's easy, but i more question. Do i not need the keepaspectratio to make sure it stays intact ? and 2: is there a way that the icon does not appear in the table of content ?
    – user76557
    Apr 19, 2015 at 1:31
  • @user2475269 First, when you specify only the height (or width) the aspect ratio is maintained. You need keepaspectratio only when you specify both width and height. Second, you can use an optional argument for sub section like \subsection[Sentinel]{Sentinel \icon{example-image-a}} so that picture doesn't appear in toc or heading etc.
    – user11232
    Apr 19, 2015 at 1:36
  • Great, I'm accepting as soon as i can
    – user76557
    Apr 19, 2015 at 1:37

You must log in to answer this question.