cgnieder / acro

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

Error when using acronym in heading with fancyhdr #145

Closed cgnieder closed 4 years ago

cgnieder commented 4 years ago

Original report by Willem Duncan (Bitbucket: [Willem Duncan](https://bitbucket.org/Willem Duncan), ).


When using acronyms in headings that get used by fancyhdr, an error occurs:

! Package acro Error: You've requested acronym `CD' on line 10 but you apparently haven't defined it, yet!

! Missing number, treated as zero.

This seems to be caused by fancyhdr attempting to write the current chapter/section/subsection (depending on configuration) in the header text. Removing the acronym from the section title resolves the issue, as does changing fancyhdr configuration to use a different text.

MWE:

\documentclass{article}
\usepackage{acro}
\usepackage{fancyhdr}
\pagestyle{fancy}

\DeclareAcronym{cd}{short = cd, long = Compact Disc , short-format = \scshape}

\begin{document}
\section{Lorem ipsum \ac{cd}}
\end{document}

cgnieder commented 4 years ago

Original comment by Clemens Niederberger (Bitbucket: cgnieder, GitHub: cgnieder).


Interesting… Thanks for the report

cgnieder commented 4 years ago

Original comment by Willem Duncan (Bitbucket: [Willem Duncan](https://bitbucket.org/Willem Duncan), ).


Was thinking about it a bit more and tested without fancyhdr, using the normal TeX \pagestyle{headings}. The same error occurs.

\documentclass{article}
\usepackage{acro}
\pagestyle{headings}

\DeclareAcronym{cd}{short = cd, long = Compact Disc , short-format = \scshape}

\begin{document}
\section{Lorem ipsum \ac{cd}}
\end{document}

Package acro Error: You've requested acronym `CD' on line 9 but youType to continue. \end{document}

cgnieder commented 4 years ago

Original comment by Clemens Niederberger (Bitbucket: cgnieder, GitHub: cgnieder).


I have been blind :see_no_evil:

The acronym is defined through \DeclareAcronym{cd} (i.e. with lowercase cd) but the error complains “You've requested acronym `CD'”, i.e. with an uppercase CD.

This is because the pagestyle headings is defined with \MakeUppercase. The easiest way out is to define the acronym with an uppercase ID:

\documentclass{article}
\usepackage[T1]{fontenc}
\usepackage{acro}
\pagestyle{headings}

\DeclareAcronym{CD}{
  short = cd ,
  long = Compact Disc ,
  short-format = \scshape
}

\begin{document}
\section{Lorem ipsum \acs*{CD}}
\end{document}

Another but more complicated version is to use the textcase package with the overload option. This will redefine \MakeUppercsse to act like \MakeTextUppercase (which then will also treat math correct). Then the uppercasing can be prevented with \NoCaseChange{} (which unfortunately also must be protected):

\documentclass{article}
\usepackage[T1]{fontenc}
\pagestyle{headings}

\usepackage[overload]{textcase}

\usepackage{acro}
\DeclareAcronym{cd}{
  short = cd ,
  long = Compact Disc ,
  short-format = \scshape
}

\begin{document}

\section{Lorem Ipsum \protect\NoCaseChange{\acs*{cd}}}

\end{document}

cgnieder commented 4 years ago

Original comment by Clemens Niederberger (Bitbucket: cgnieder, GitHub: cgnieder).


Since this is no actual bug in acro I'll close this as “invalid”…

cgnieder commented 4 years ago

Original comment by Willem Duncan (Bitbucket: [Willem Duncan](https://bitbucket.org/Willem Duncan), ).


Hey, good find, I guess we’re both blind :speak_no_evil: Thanks for digging into this. However, I disagree on closing this issue, though it does turn into more of a feature request rather than a bug.

Case changes in headers are common, and (apparently) they also change the case of command arguments, which makes sense in a way and is very unlikely to change in the future. Page headers might also not be the only times when case is changed automatically. Depending on styles it could also be normal headings, table of contents, parts of beamer slides, and so on. And each time, it’s changing something unrelated to acro that will cause a previously or otherwise working acronym to stop working and give an error.

Worst case, multiple acronyms might be defined with the same key using only different capitalization, and this would cause a different definition to be used without any warning or error to the user. Imagine the following:

\DeclareAcronym{cd}{
  short = cd ,
  long = Compact Disc
}
\DeclareAcronym{CD}{
  short = CD,
  long = Corps Diplomatique
}

....

\section{Lorem Ipsum \ac{cd}}
Lorem Ipsum \ac{cd}.

Assuming uppercase conversion of section titles, this would give a different meaning to the first and second time of using exactly the same acronym.

Dismissing this issue therefore might not be the best solution. Since your acronyms are case sensitive, wouldn’t it be a good option to include the \protect\NoCaseChange{...} into acro? Either through referencing textcase or through cloning the relevant code.

Alternatively (but breaking compatibility in cases of overlap) make the key be case insensitive. I just quickly checked, and I found no reference in the acro manual to key being case sensitive, though I might have overlooked it. I would personally prefer this variant: having acronyms that differ only in key casing is waiting for problems to happen, anyway, and should be discouraged.

cgnieder commented 4 years ago

Original comment by Clemens Niederberger (Bitbucket: cgnieder, GitHub: cgnieder).


The ID of an acronym is case sensitive, that’s true. I’m hesitant to change this, though.

Simply incorporating \protect\NoCaseChamge{…} as you suggest won’t help as the following piece of code demonstrates:

\documentclass{article}
\pagestyle{headings}
\usepackage[overload]{textcase}

\usepackage{etoolbox}
\newrobustcmd*\ac[1]{\protect\NoCaseChange{#1} vs.\@ #1?}

\begin{document}

\section{Lorem Ipsum \ac{cd}}

\end{document}

There is simply nothing that can be done from preventing the uppercasing other than redefining \MakeUppercase. Since it is a core LaTeX command that is out of the question. And because you are the first reporting this issue since the firts release of acro back in 2011 I doubt that it is a common problem, anyway.

cgnieder commented 4 years ago

Original comment by Willem Duncan (Bitbucket: [Willem Duncan](https://bitbucket.org/Willem Duncan), ).


Maybe I’m just the first one to report it :blush: Many acronyms are completely uppercase by definition, thus not noticeably affected by \MakeUppercase.

As said, I would change the keys to be case insensitive by default. Consider a package option to make them case sensitive to support the (hopefully very few) users for which this change causes issues. But of course, that choice is of course completely up to you, and as mentioned before it is not an easy one to make. If only packages came with usage metrics!

Another approach might be to change the error message? I’m unfamiliar with LaTeX programming, so not sure of the effort involved. But maybe when a key lookup error happens, make a second case-insensitive lookup, and in case of a result display something like “You've requested acronym `CD' on line 10 but only `cd' is defined, check “.

And last, please at least add the case-sensitivity behavior (and maybe an example of a workaround) to the documentation.

Thanks for the responses and for the excellent package. The full complement of pluralization and sorting options, and the longtable/tabu support made it invaluable for the last project.

cgnieder commented 4 years ago

Original comment by Clemens Niederberger (Bitbucket: cgnieder, GitHub: cgnieder).


As said, I would change the keys to be case insensitive by default.

This is a lot harder than it looks like… at least when I don’t want a whole bunch of documented user commands to change how they work.

I’ll add something to manual for people to look out.

cgnieder commented 4 years ago

Original comment by Clemens Niederberger (Bitbucket: cgnieder, GitHub: cgnieder).


This is a lot harder than it looks like… at least when I don’t want a whole bunch of documented user commands to change how they work.

I managed to implement it anyway (commit 17c0b69):

\documentclass{article}
\pagestyle{headings}

\usepackage{acro}
\acsetup{case-sensitive=false}

\DeclareAcronym{cd}{short = CD, long = Compact Disc}

\begin{document}
\section{Lorem ipsum \acf*{cd}}

\ac{CD}

\acflike{CD}{Rohling}

\end{document}

image

This will be part of the next release.