cgnieder / acro

acronyms for LaTeX
LaTeX Project Public License v1.3c
41 stars 9 forks source link

patch/caption should take effect even without the caption package #201

Closed bersbersbers closed 3 years ago

bersbersbers commented 3 years ago
\documentclass{article}
%\usepackage{caption}
\usepackage{acro}
\acsetup{patch/floats=false, patch/caption=true}
\DeclareAcronym{id}{short=short, long=long}
\begin{document}
\begin{figure}
    \caption{\ac{id} and \ac{id} in a short line}
\end{figure}
\acresetall
\begin{figure}
    \caption{\ac{id} and \ac{id} in another, much longer line that breaks because it's long}
\end{figure}
\end{document}

Regardless of the value of patch/floats, I do not see a difference between patch/caption=true and patch/caption=false, although for the long caption, the behavior without patch/caption is the same with and without \usepackage{caption}, respectively. What I am saying is: patch/caption should apply also to regular captions, at least the long ones.

What I am looking for is

long (short) and short in the long line

without loading the caption package, if possible - but I get either "short and short" (patch/floats=false) or "long and long" (patch/floats=true).


By the way: the manual says

The same is true for single line captions from the caption package.

Isn't this true for all captions with the caption package? See this example:

\documentclass{article}
\usepackage{caption}
\usepackage{acro}
\acsetup{patch/floats=false, patch/caption=false}
\DeclareAcronym{id}{short=short, long=long}
\begin{document}
\begin{figure}
    \caption{\ac{id} and \ac{id} in a short line}
\end{figure}
\acresetall
\begin{figure}
    \caption{\ac{id} and \ac{id} in another, much longer line that breaks because it's long}
\end{figure}
\end{document}

The problem ("short and short") appears with both captions, and is fixed for both with patch/caption=true. My understanding is that caption's singlelinecheck is done for all captions to see if they are single-line. Confirmed (by yourself, I think):

Without caption/subcaption package, the caption text will be evaluated one time if it fits into a single line and twice if it does not. When using the caption/subcaption package the caption text will always be evaluated twice.

https://tex.stackexchange.com/a/431633/30810

Maybe you could write:

The same is true for single line captions generally, and all captions from the caption package.

bersbersbers commented 3 years ago

Related, I guess: https://tex.stackexchange.com/questions/433322/why-does-latex-step-the-footnote-counter-twice-in-a-two-line-caption#comment1086826_433335

cgnieder commented 3 years ago

patch/caption only applies to the caption package. What it does adding \acswitchoff to caption's \caption@prepareslc. It cannot have any effects if the package is not loaded.

I've decided not to check other definitions of \caption as they depend on the document class.

For standard classes \@makecaption is defined as follows:

\long\def\@makecaption#1#2{%
  \vskip\abovecaptionskip
  \sbox\@tempboxa{#1: #2}%
  \ifdim \wd\@tempboxa >\hsize
    #1: #2\par
  \else
    \global \@minipagefalse
    \hb@xt@\hsize{\hfil\box\@tempboxa\hfil}%
  \fi
  \vskip\belowcaptionskip}

This shows exactly what you observe: long captions are typeset twice, once for measuring in the box and once for real. For short captions the box is just reinserted. With a standard class (article, report, or book) one could do

\makeatletter
\renewcommand\@makecaption[2]{%
  \vskip\abovecaptionskip
  \sbox\@tempboxa{\acswitchoff#1: #2}%
  \ifdim \wd\@tempboxa >\hsize
    #1: #2\par
  \else
    \global \@minipagefalse
    \hb@xt@\hsize{\hfil#1: #2\hfil}%
  \fi
  \vskip\belowcaptionskip}
\makeatother

but this relies very much in the implementation which is why I'm hesitant to do so in acro. The above would not work with the classes from tufte-latex or with memoir or the koma-script classes. Or. It might work but at the same time destroy how these classes make their captions. At the very least it would disable their options and modifications around captions.

bersbersbers commented 3 years ago

Thanks! For now, in my document, I use this workaround:

\usepackage{regexpatch}
\makeatletter
\newcommand{\mlcaption}[1]{
    \makeatletter
    \regexpatchcmd{\@makecaption}{
        (\c{@tempboxa} \cB\{) \#1:\ \#2 (\cE\})
    }{
        \1 \c{hspace} \cB\{ 2 \c{hsize} \cE\} \2
    }{}{Error.}
    \caption{#1}
    \makeatother
}
\makeatother

This forces the single-line-check to see a wide line regardless of the content.

And then I use \mlcaption for multi-line captions (which they almost always are).

bersbersbers commented 3 years ago

And as I had to notice, this workaround had to be adapted for ieeecolor.cls as you rightly explain. [Fortunately, that document class has some \ifcenterfigcaptions which is false for me, so I can simply patch its \@makecaption globally.] Thanks again!