2

As @egreg notes in this answer to Can't compile XeLaTeX document with biblatex and gb4e together, the package gb4e makes _ and ^ active to be able to use subscripts and superscripts in ordinary text without having to switch to math mode. Unsurprisingly, this can cause problems, which is why the package gb4e has a way of turning this behavior off with \noautomath.

Given this, the following MWE does not compile unless \noautomath is uncommented.

\documentclass{article}

\usepackage{mwe}
\usepackage{graphicx}

\usepackage{gb4e}
%\noautomath

\begin{document}

\begin{figure}
\centering
\includegraphics{example-image-a}
\caption[$F_{0}$]{$F_{0}$}
\end{figure}

\end{document}

Although, another way to make the MWE compile while leaving \noautomath commented is to change the \caption line to something like \caption[No subscript here]{F$_{0}$}.

My question is thus: why does making _ active break its usage in \caption?

1 Answer 1

5

The definition of _ as an active character is fragile, so it can't appear in moving arguments.

If you don't want to use \noautomath, you have to remember putting \protect in front of _, when in a moving argument, or use \sb:

\caption[$F\protect_{0}$]{$F_{0}$}
\caption[$F\sb{0}$]{$F_{0}$}

Note that the moving argument is the optional one, in case it's specified.

If the package defined \automath with \DeclareRobustCommand_[1]{...} instead of \def_##1{...} there would be no problem. So this might be a suggestion for the package maintainers.

Another workaround is to \robustify the active characters with the help of etoolbox:

\documentclass{article}

\usepackage{etoolbox}
\usepackage{graphicx}

\usepackage{gb4e}

\AtBeginDocument{%
  \begingroup\lccode`~=`_ \lowercase{\endgroup\robustify~}%
  \begingroup\lccode`~=`^ \lowercase{\endgroup\robustify~}%
}

\begin{document}

x_y x^y_z

\begin{figure}
\centering
\includegraphics{example-image-a}
\caption[$F_{0}$]{$F_{0}$}
\end{figure}

\end{document}

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .