
Я пытаюсь вставить значок ( 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}
Или используйте \newrobustcmd
( etoolbox
снова) как
\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}