3

In the following example, why does the word Spam appear four timestimestimestimes?

$ cat mwe.tex 
\documentclass{beamer}
\usepackage{breqn}
\newcommand{\spam}{\textrm{Spam}}
\begin{document}
\begin{frame}
\[ \spam \]
\end{frame}
\end{document}

Resulting PDF converted to PNG

...and what can I do to get it just once?

I'm using:

  • LuaTeX, Version beta-0.80.0 (TeX Live 2015) (rev 5238),
  • beamer 2015/01/05 3.36,
  • breqn 2015/08/11 v0.98d.
8
  • Can't reproduce. Which tex version are you using? I tried with TeXLive 206 Jul 4, 2016 at 14:04
  • 1
    @samcarter The problem is caused by compilation with LuaTeX, something around \mathchoice. Jul 4, 2016 at 14:05
  • @samcarter Added versions for luatex, beamer, breqn.
    – gerrit
    Jul 4, 2016 at 14:06
  • 1
    Why don't you use \mathrm instead of \textrm? Jul 4, 2016 at 14:08
  • @UlrikeFischer Because of ignorance / old bad habits / bad habits inherited from others / I don't know. Just now learning from your helpful answer here on the difference. It appears \mathrm does not show the problem that \textrm does, so there is the answer to part 2 of my question.
    – gerrit
    Jul 4, 2016 at 14:09

1 Answer 1

4

The problem is triggered, if the redefinition of \mathchoice in package mathstyle meets LuaTeX's \mathstyle, from package mathstyle:

\def\mathchoice{%
  \relax\ifcase\mathstyle
    \expandafter\@firstoffour % Display
  \or
    \expandafter\@firstoffour % Cramped display
  \or
    \expandafter\@secondoffour % Text
  \or
    \expandafter\@secondoffour % Cramped text
  \or
    \expandafter\@thirdoffour % Script
  \or
    \expandafter\@thirdoffour % Cramped script
  \else
    \expandafter\@fourthoffour % (Cramped) Scriptscript
  \fi
}

\ifcase expects a number and continues the expansion until a token is found, which does not contain to a number (non-digit). Thus the first \expandafter of case "0" for "display style" is called at the wrong time, when the number is still being read. A \relax stops that and should fix the issue:

\documentclass{beamer}
\usepackage{breqn}

\makeatletter
\def\mathchoice{%
  \relax\ifcase\mathstyle\relax
    \expandafter\@firstoffour % Display
  \or
    \expandafter\@firstoffour % Cramped display
  \or
    \expandafter\@secondoffour % Text
  \or
    \expandafter\@secondoffour % Cramped text
  \or
    \expandafter\@thirdoffour % Script
  \or
    \expandafter\@thirdoffour % Cramped script
  \else
    \expandafter\@fourthoffour % (Cramped) Scriptscript
  \fi
}
\makeatother

\newcommand{\spam}{\textrm{Spam}}
\begin{document}
\begin{frame}
\centering

$\displaystyle \mathchoice{D}{T}{S}{s}$

\[ \spam \]
\end{frame}
\end{document}

Result

1
  • Same answer here
    – egreg
    Jul 4, 2016 at 14:39

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