4

The following code is continued from this post and this one. The following code compiles ok without babel but not with it. See the console output below. Is there a way resolve this issue?

\documentclass{article}
\usepackage{afterpage}
\usepackage[french]{babel} % no error if uncommented
\usepackage{mwe}
\usepackage{refcount}
\usepackage{tocloft}
\usepackage{xparse}
\usepackage{xwatermark} % ruled out

\newlistof{encl}{encl}{List of enclosed items}

\NewDocumentCommand
{\addencl}
{ m }
{
  \refstepcounter{encl}
  \addcontentsline{encl}{encl}{\theencl. #1}
}

\NewDocumentCommand
{\getencl}
{ m }
{
  E{\small NCL} \# \getrefnumber{#1}
}

\begin{document}
\newwatermark*[page=\getpagerefnumber{encl:bar}]{Bar}
\newwatermark*[page=\getpagerefnumber{encl:foo}]{Foo}

\listofencl

%\verb|\getrefnumber{encl:foo}|:\getrefnumber{encl:foo}
%\verb|\getpagerefnumber{encl:foo}|:\getpagerefnumber{encl:foo}

\getencl{encl:bar}~is~on~page~\getpagerefnumber{encl:bar}

\getencl{encl:foo}~is~on~page~\getpagerefnumber{encl:foo}

\newpage

%\refstepcounter{encl}
\addencl{Bar}\label{encl:bar}
\includegraphics[width=\linewidth]{example-image-a}

\newpage

\addencl{Foo}\label{encl:foo}
\includegraphics[width=\linewidth]{example-image-b}

%\newwatermark*{}


\end{document}

Console output:

ERROR: Missing \endcsname inserted.

--- TeX said --- \unskip l.36 \getencl{encl:bar} ~is~on~page~\getpagerefnumber{encl:bar} --- HELP --- From the .log file...

The control sequence marked should not appear between \csname and \endcsname.

2 Answers 2

4

When babel-french is active, the colon has a special meaning: “remove a possible space, add a nonbreaking space and print the colon”.

Indeed, you get

! Missing \endcsname inserted.
<to be read again> 
                   \unskip 

LaTeX makes provision so that \ref or \pageref take care of babel shorthands such as :, ? and ! for French and " for German. This cannot be done with \getrefnumber or \getpagerefnumber, because the protection needs assignments, in this case setting a switch so that the colon shorthand, for instance, produces a standard colon.

The refcount commands want and need to be fully expandable and so no assignment can take place during their job: expandability is the feature for which refcount was born.

In general, I recommend avoiding babel shorthands in labels.

For your specific problem, where you use no macros in the argument to \get(page)refnumber, you can use \detokenize:

\documentclass{article}
\usepackage[french]{babel} % no error if uncommented
\usepackage{refcount}
\usepackage{tocloft}
\usepackage{xparse}
\usepackage{xwatermark} % ruled out

\newlistof{encl}{encl}{List of enclosed items}

\NewDocumentCommand{\addencl}{ m }
 {%
  \refstepcounter{encl}%
  \addcontentsline{encl}{encl}{\theencl. #1}%
 }

\NewDocumentCommand{\getencl}{ m }
 {%
  E{\small NCL} \# \getrefnumber{\detokenize{#1}}%
 }

\newcommand{\bgetpagerefnumber}[1]{\getpagerefnumber{\detokenize{#1}}}

\begin{document}

\newwatermark*[page=\bgetpagerefnumber{encl:bar}]{Bar}
\newwatermark*[page=\bgetpagerefnumber{encl:foo}]{Foo}

\listofencl

\getencl{encl:bar}~is~on~page~\bgetpagerefnumber{encl:bar}

\getencl{encl:foo}~is~on~page~\bgetpagerefnumber{encl:foo}

\newpage

%\refstepcounter{encl}
\addencl{Bar}\label{encl:bar}
\includegraphics[width=\linewidth]{example-image-a}

\newpage

\addencl{Foo}\label{encl:foo}
\includegraphics[width=\linewidth]{example-image-b}

%\newwatermark*{}


\end{document}

But it's much simpler using encl-foo.

3

It will work if you use \string: in place of : at a number of locations.

\documentclass{article}
\usepackage{afterpage}
\usepackage[french]{babel} % no error if uncommented
\usepackage{mwe}
\usepackage{refcount}
\usepackage{tocloft}
\usepackage{xparse}
\usepackage{xwatermark} % ruled out

\newlistof{encl}{encl}{List of enclosed items}

\NewDocumentCommand
{\addencl}
{ m }
{
  \refstepcounter{encl}
  \addcontentsline{encl}{encl}{\theencl. #1}
}

\NewDocumentCommand
{\getencl}
{ m }
{
  E{\small NCL} \# \getrefnumber{#1}
}

\begin{document}
\newwatermark*[page=\getpagerefnumber{encl\string:bar}]{Bar}
\newwatermark*[page=\getpagerefnumber{encl\string:foo}]{Foo}

\listofencl

%\verb|\getrefnumber{encl:foo}|:\getrefnumber{encl:foo}
%\verb|\getpagerefnumber{encl:foo}|:\getpagerefnumber{encl:foo}

\getencl{encl\string:bar}~is~on~page~\getpagerefnumber{encl\string:bar}

\getencl{encl\string:foo}~is~on~page~\getpagerefnumber{encl\string:foo}

\newpage

%\refstepcounter{encl}
\addencl{Bar}\label{encl:bar}
\includegraphics[width=\linewidth]{example-image-a}

\newpage

\addencl{Foo}\label{encl:foo}
\includegraphics[width=\linewidth]{example-image-b}

%\newwatermark*{}


\end{document}

Output:

enter image description here

Note: I initially added \detokenize to the document commands defined, but there remained a problem with \getpagerefnumber.

You must log in to answer this question.

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