3

Please see this MWE.

\documentclass{memoir}

% Adapted from https://tex.stackexchange.com/a/176815/14103
\usepackage{tikz}
\usepackage{xfrac}
\usetikzlibrary{fpu}
\newcommand{\dectofrac}[1]{\begingroup%
  \pgfkeys{/pgf/number format/frac}% Format as fraction
  \let\frac\sfrac% Let \frac act like \sfrac
  \pgfmathprintnumber{#1}\endgroup}

\begin{document}

\chapter{First}
\chapterprecishere{\dectofrac{0.25}}
\chapterprecistoc{\dectofrac{0.25}} % Works fine when this line is
                                    % commented out or the \dectofrac
                                    % is not used

\end{document}

Compilation causes the process to run indefinitely without any error message. We can exit only using Ctrl-C which shows the following as terminal output:

Interruption.
\pgfkeys@parse ...uturelet \pgfkeys@possiblerelax 
                                                  \pgfkeys@parse@main 
l.16 \chapterprecistoc{\dectofrac{0.25}}

When the line containing \chapterprecistoc is commented out compilation proceeds without the slightest error. The culprit here is the \dectofrac command since the \chapterprecistoc will work fine if we don't use this command. I am otherwise clueless here.

Masroor
  • 17,842

1 Answers1

4

You can declare a robust command \inlinedectofrac, which can be used inside of \chapterprecistoc. Or you can use \protect before \dectofrac.

For more Information see What is the difference between Fragile and Robust commands?

\documentclass{memoir}

% Adapted from https://tex.stackexchange.com/a/176815/14103
\usepackage{tikz}
\usepackage{xfrac}
\usetikzlibrary{fpu}
\newcommand{\dectofrac}[1]{\begingroup%
  \pgfkeys{/pgf/number format/frac}% Format as fraction
  \let\frac\sfrac% Let \frac act like \sfrac
  \pgfmathprintnumber{#1}\endgroup}

\DeclareRobustCommand{\inlinedectofrac}[1]{%
  \dectofrac{#1}%
}

\begin{document}

\tableofcontents

\chapter{First}
\chapterprecistoc{\inlinedectofrac{0.25}}

\chapter{Second}
\chapterprecistoc{\protect\dectofrac{0.75}}

\end{document}

enter image description here

Alternatively, use \NewDocumentCommand, which is available as xfrac loads xparse; this doesn't need \protect or another command.

\documentclass{memoir}

% Adapted from https://tex.stackexchange.com/a/176815/14103
\usepackage{tikz}
\usepackage{xfrac}
\usetikzlibrary{fpu}
\NewDocumentCommand{\dectofrac}{m}{\begingroup%
  \pgfkeys{/pgf/number format/frac}% Format as fraction
  \let\frac\sfrac% Let \frac act like \sfrac
  \pgfmathprintnumber{#1}\endgroup}

\begin{document}

\tableofcontents

\chapter{First}
\chapterprecistoc{\dectofrac{0.25}}

\chapter{Second}
\chapterprecistoc{\dectofrac{0.75}}

\end{document}
egreg
  • 1,121,712
dexteritas
  • 9,161
  • 1
    \NewDocumentCommand{\dectofrac}{m}{<same code>} would be sufficient: it doesn't require \protect or another command. It is available because xfrac loads xparse. – egreg Jul 24 '17 at 09:02
  • Not really like you did: <same code> referred to the original code in the definition of \dectofrac. I added the correct code. – egreg Jul 24 '17 at 09:21