如何在部分名稱中包含圖形

如何在部分名稱中包含圖形

我試圖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}}

ETC....

如果您正在使用,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}

在此輸入影像描述

相關內容