6

The following MWE example demonstrates the problem:

\documentclass{article}

\makeatletter
\def\arctg{\mathop{\operator@font ArcTg}\nolimits}
\makeatother

\usepackage{ifxetex,ifluatex}
\ifxetex
    \usepackage{fontspec}
    \setmainfont{Cambria}
    \usepackage{unicode-math}
    \setmathfont{Cambria Math}
\else\ifluatex
    \usepackage{fontspec}
    \defaultfontfeatures{Renderer=Basic,Ligatures={TeX}}
    \usepackage{unicode-math}
    \setmathfont{Cambria Math}
\else
    \usepackage[T1,T2A]{fontenc}
    \usepackage[utf8]{inputenc}
\fi\fi

\begin{document}
\ifxetex
    This is Xe\LaTeX
\else\ifluatex
    This is Lua\LaTeX
\else
    This is pdf\LaTeX
\fi\fi

 \subsubsection{Function $arctg(x)$}

 Function
 \begin{equation}
 \arctg(x)
 \end{equation}

 \subsubsection{Function $\cos(x)$}

 \begin{equation}
 \cos(x)
 \end{equation}

 \tableofcontents

\end{document}

I tried both Miktex and TL. At first run XeTeX produces .toc file with extremely long record:

\contentsline {subsubsection}{\numberline {0.0.2}Function $\mathop {\Umathcode 97=7\symoperators 97\scan _stop: \Umathcode 98=7\symoperators 98\scan _stop:........

(here less than 1 percent is shown). At the second run xetex fails to finish the compilation. I did not chech if LuaLatex also fails. The compilation succeeds if unicode-math is not loaded.

Zarko
  • 296,517
  • 1
    off-topic: is\DeclareMathOperator{\arctg}{ArcTg} not suficient? – Zarko Aug 10 '17 at 03:51
  • And what exactly is the problem? If you want just to get rid of the error then use \protect\sin in section headers. Using the amsmath package also fixes this error by redefining \sin in another way (which is compatible with unicode-math). Or do you want to understand what happens inside the definition and why it expands? – Sergei Golovan Aug 10 '17 at 06:15
  • It should be \subsubsection{Function $\arctg(x)$} rather than \subsubsection{Function $arctg(x)$}, right? What happens if you insert the missing backslash character? – Mico Aug 10 '17 at 07:14
  • @Mico: The same error as for \sin happens, which is expected because the definition is basically the same. – Sergei Golovan Aug 10 '17 at 08:37
  • 1
    Without amsmath the commands such as \sin are all fragile, so they should be prefixed by \protect when in moving arguments. – egreg Aug 10 '17 at 16:08

2 Answers2

4

Commands such as \cos (operator names) are fragile, so they should be prefixed by \protect.

With amsmath the problem is milder, because \cos expands to \qopname\relax{o}{cos} and `\qopname is robust.

In your setting, you should do

\AtBeginDocument{%
  \@ifpackageloaded{amsopn}
    {\DeclareMathOperator{\arctg}{arctg}}%
    {\def\arctg{\mathop{\operator@font arctg}\nolimits}}%
}

so as to uniformize the definitions.

You can also make the commands robust:

\documentclass{article}

\usepackage{ifxetex,ifluatex}
\ifxetex
    \usepackage{fontspec}
    \usepackage{unicode-math}
\else\ifluatex
    \usepackage{fontspec}
    \usepackage{unicode-math}
\else
    \usepackage[T1,T2A]{fontenc}
    \usepackage[utf8]{inputenc}
\fi\fi

\makeatletter
\AtBeginDocument{%
  \@ifpackageloaded{amsopn}
    {\DeclareMathOperator{\arctg}{arctg}}%
    {\def\arctg{\mathop{\operator@font arctg}\nolimits}}%
}
\def\protectoperators#1{%
  \AtBeginDocument{%
    \@tfor\next:=#1\do{\expandafter\MakeRobust\next}%
  }
}
\makeatother

\protectoperators{%
  \sin\cos\log\ln\arctg % add the other operators
}


\begin{document}

\show\sin

\ifxetex
    This is Xe\LaTeX
\else\ifluatex
    This is Lua\LaTeX
\else
    This is pdf\LaTeX
\fi\fi

\subsubsection{Function $\arctg(x)$}

Function
\begin{equation}
\arctg(x)
\end{equation}

\subsubsection{Function $\cos(x)$}

\begin{equation}
\cos(x)
\end{equation}

\tableofcontents

\end{document}

Here is the contents of the .toc file:

\contentsline {subsubsection}{\numberline {0.0.1}Function $\arctg (x)$}{1}
\contentsline {subsubsection}{\numberline {0.0.2}Function $\cos (x)$}{1}
egreg
  • 1,121,712
  • Thank you a lot. Could you explain why log-like functions were not made robust by fixltx2 as it was done with many orther fragile commands? – Igor Kotelnikov Aug 11 '17 at 12:42
  • @IgorKotelnikov No idea. The fragility turns out to only be really harmful only with unicode-math and without amsmath. – egreg Aug 11 '17 at 12:44
1

(too long for a comment, hence posted as an answer)

I don't understand why you don't use the \DelareMathOperator machinery (provided by the amsmath package) to generate the macro \arctg. And, I don't think much good is done anymore by specifying the option Renderer=Basic under LuaLaTeX. At any rate, the following, somewhat streamlined version of your MWE runs flawlessly under pdfLaTeX, XeLaTeX, and LuaLaTeX.

\documentclass{article}
\usepackage{amsmath} % for "\DeclareMathOperator" macro
\DeclareMathOperator{\arctg}{ArcTg}

\usepackage{ifxetex,ifluatex}
\ifxetex
      \usepackage{unicode-math}
      \setmainfont{Cambria}
      \setmathfont{Cambria Math}
\else
   \ifluatex
      \usepackage{unicode-math}
      \setmainfont[BoldFont={Cambria Bold}]{Cambria}
      \setmathfont{Cambria Math}
   \else
      \usepackage[T2A,T1]{fontenc}
      \usepackage[utf8]{inputenc}
   \fi
\fi

\begin{document}
\noindent
\ifxetex
      This is Xe\LaTeX
\else
   \ifluatex
      This is Lua\LaTeX
   \else
      This is pdf\LaTeX
   \fi
\fi

\tableofcontents
\medskip

\section{Function $\arctg(x)$}
\begin{equation} \arctg(x) \end{equation}

\section{Function $\cos(x)$} 
\begin{equation} \cos(x)   \end{equation}

\end{document} 
Mico
  • 506,678
  • Indeed, loading amsmath removes the error. But I cannot accept this as an answer. I need a code to work from within a language module for babel where call for amsmath would be bad solution. As to Renderer=Basic, lualatex just ignores it issuing a warning. I guess that unicode-math must be fixed to resolve the problem. – Igor Kotelnikov Aug 10 '17 at 12:16
  • @IgorKotelnikov - I'm not sure what your concern is over the use of the amsmath package -- quite likely because I have no idea what your babel module is supposed to do. To me, the main issue raised by your posting is that the unicode-math package appears to make assumptions about what is valid input; loading the amsmath appears to "help" unicode-math. These issues would appear to apply whether or not babel is loaded, no? In general, I can't think of serious downsides to simply recommending that whenever unicode-math is loaded, amsmath should be loaded as well. Do you disagree? – Mico Aug 10 '17 at 19:47
  • Yes, this issue appears no matter if babel is called or not. – Igor Kotelnikov Aug 10 '17 at 22:51