2

Control sequences with optional arguments inside optional arguments must be "hidden" with braces. Using the abntex2cite package to handle my citations, I often want to write:

\begin{teor}[{\cite[p.~42]{johndoe1999}}]
  $2+2=4$.
\end{teor}

That doesn't work, however; I need to write:

\begin{teor}[\leavevmode{\cite[p.~42]{johndoe1999}}]
  $2+2=4$.
\end{teor}

instead. \cite and \citeonline behave exactly the same in this regard. Besides \leavevmode, a \strut or just a space/end-of-line also work (except the space looks weird/ugly). That is, LaTeX needs something to start a line right before the citation macro between braces. I thought that maybe this happens because the macros \cite and \citeonline had to start with \leavevmode, but don't; however, the source of abntex2cite shows that the very first line of \citeonline IS \leavevmode!

So my questions are:

  • Why is another \leavevmode before \citeonline (or \cite) necessary, even though \citeonline starts with \leavevmode?
  • Is there a way to redefine \cite and \citeonline such that I don't need space/\strut/\leavevmode before the braced citation macro?

(Files to showcase what I described)

Contents of sample.tex:

\documentclass{article}
\usepackage{ntheorem}
\newtheorem{teor}{Teorema}
\newcommand*{\page}[1]{p.~#1}
\usepackage[alf]{abntex2cite}


\begin{document}
Regular ``external'' citation according to the ABNT (Brazilian) standard:
\cite{johndoe1999}.

Regular ``inline'' citation according to the ABNT (Brazilian) standard:
\citeonline{johndoe1999}.

So far, so good; no extra braces needed.
Whithin the optional argument of an environment, however,
extra braces are needed:

\begin{teor}[This works
  {\cite[\page{25}]{johndoe1999}}%<-- notice the extra braces!
  ]
  Notice the extra braces in the optional argument.
\end{teor}

\begin{teor}[Also works
  {\citeonline[\page{25}]{johndoe1999}}%<-- notice the extra braces!
  ]
  Notice the extra braces in the optional argument.
\end{teor}

And the next one works, but it requires
(besides the extra braces)
something to start a line before \verb|\cite| or \verb|\citeonline|:
it can be a space, a \verb|\strut| or \verb|\leavevmode|.
I think that's annoying and error-prone;
is there another way?

\begin{teor}[% avoid extraneous space here
  \leavevmode{\citeonline[\page{25}]{johndoe1999}}%<-- \leavevmode + extra braces!
  ]
  This one requires both
  extra braces AND ``something to start a line''
  in the optional argument.
\end{teor}

\bibliography{bibfile}
% this is how a chapter of a book is supposed to look in the ABNT style...
\end{document}

% -- the following won't compile!
% erase the previous 2 macros to generate errors
\begin{teor}[% lacks \leavevmode AND extra braces
  \citeonline[\page{25}]{johndoe1999}%
  ]
  \fbox{content}
\end{teor}

\begin{teor}[% lacks extra braces: {\citeonline...}
  \leavevmode\citeonline[\page{25}]{johndoe1999}%
  ]
  \fbox{content}
\end{teor}

\begin{teor}[% lacks \leavevmode (or \strut)
  {\citeonline[\page{25}]{johndoe1999}}%
  ]
  \fbox{content}
\end{teor}

\bibliography{bibfile}
\end{document}

Contents of bibfile.bib:

% Encoding: UTF-8
@InCollection{johndoe1999,
  author    = {Doe, John},
  booktitle = {Handbook of Foobar},
  publisher = {Barbaz},
  title     = {Interesting chapter},
  year      = {1999},
  address   = {Earth},
  editor    = {{Mc}Editor, Editor},
  pages     = {283--342},
}

problem showcase

1
  • 1
    I'm not familiar with the document class you're using, but I would look at the definition of \newtheorem and friends to see if something there might be responsible for disabling \leavevmode if the first thing within the optional argument is an open brace. (After all, when the optional argument starts with text, it works.) Commented Jul 29, 2022 at 1:51

1 Answer 1

3

The problem is obviously due to ntheorem: if I remove it, the issue disappears.

If you really need it (personally, I wouldn't use it), I can propose a workaround:

\begin{filecontents*}{\jobname.bib}
@InCollection{johndoe1999,
  author    = {Doe, John},
  booktitle = {Handbook of Foobar},
  publisher = {Barbaz},
  title     = {Interesting chapter},
  year      = {1999},
  address   = {Earth},
  editor    = {{Mc}Editor, Editor},
  pages     = {283--342},
}
\end{filecontents*}

\documentclass{article}
\usepackage[alf]{abntex2cite}
\usepackage{ntheorem}

\newtheorem{teor}{Teorema}

\ExplSyntaxOn
\NewDocumentCommand{\fixtheorem}{m}
 {
  \cs_set_eq:cc { #1@fixed } { #1 }
  \cs_set_eq:cc { end#1@fixed } { end#1 }
  \RenewDocumentEnvironment{#1}{o}
   {
    \IfNoValueTF{##1}{\use:c{#1@fixed}}{\use:c{#1@fixed}[\exp_not:n{##1}]}
   }
   {
    \use:c{end#1@fixed}
   }
 }
\ExplSyntaxOff

\fixtheorem{teor}

\newcommand*{\page}[1]{p.~#1}

\begin{document}

Regular ``external'' citation according to the ABNT (Brazilian) standard:
\cite{johndoe1999}.

Regular ``inline'' citation according to the ABNT (Brazilian) standard:
\citeonline{johndoe1999}.

So far, so good; no extra braces needed.
Whithin the optional argument of an environment, however,
extra braces are needed:

\begin{teor}[This works {\cite[\page{25}]{johndoe1999}}]
Notice the extra braces in the optional argument.
\end{teor}

\begin{teor}[Also works {\citeonline[\page{25}]{johndoe1999}}]
Notice the extra braces in the optional argument.
\end{teor}

And the next one works, but it requires
(besides the extra braces)
something to start a line before \verb|\cite| or \verb|\citeonline|:
it can be a space, a \verb|\strut| or \verb|\leavevmode|.
I think that's annoying and error-prone;
is there another way?

\begin{teor}[{\cite[\page{25}]{johndoe1999}}]
This one requires both extra braces AND ``something to start a line'' 
in the optional argument.
\end{teor}

\begin{teor}[{\citeonline[\page{25}]{johndoe1999}}]
This one requires both extra braces AND ``something to start a line'' 
in the optional argument.
\end{teor}

\bibliography{\jobname}

\end{document}

The original teor is renamed teor@fixed and a more robust teor environment is defined, with the optional argument protected from the untimely expansion done by ntheorem.

This also has a little advantage, in that you can even omit the inner braces and

\begin{teor}[\cite[\page{25}]{johndoe1999}]

would work.

enter image description here

You must log in to answer this question.

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