セクション名内にグラフィックを含める方法

セクション名内にグラフィックを含める方法

pdfセクション名のすぐ横にアイコン (ファイル) を挿入しようとしています。

ここに画像の説明を入力してください

小さな盾のアイコンを の場所に置きたいです<Icon>

この画像を生成するコードは次のとおりです。

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

\icon内部に追加しようとすると\subsection:

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

エラーが発生します。

! 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

可能であれば、アイコンが目次に表示されず、目次からのリンクが引き続き機能すると便利です。

\icon定義される

\newcommand{\icon}[1]{\begingroup
\setbox0=\hbox{\includegraphics[height=12pt,keepaspectratio]{#1}}%
\parbox{\wd0}{\box0}\endgroup}

答え1

イオンが必要です\protect。ところで、その複雑なものは\newcommand単純化できます。

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

多くの場合、それを望まない場合は\protect、次の方法があります。まず、 をrobust使用して作成します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}

または\newrobustcmdetoolbox再び)のように使います

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

または、次のようなオプションの引数を使用します。

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

等....

を使用している場合は、警告とその余波を取り除くためにhyperrefを使用する必要があるかもしれません。\texorpdfstring{\icon{example-image-a}}{}

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

ここに画像の説明を入力してください

関連情報