latex3 / babel

The babel system for LaTeX, LuaLaTeX and XeLaTeX
LaTeX Project Public License v1.3c
125 stars 34 forks source link

Textcase doesn't handle \label when babel is loaded, unless \label is \protected #193

Closed John02139 closed 1 year ago

John02139 commented 1 year ago

Brief outline of the bug

Under the most recent releases, \label must be \protect'd in uppercase environments if babel is loaded. This issue is related to issue latex3/latex2e#881. This behavior was not present in mid-September 2022.

Minimal example showing the bug

\RequirePackage{latexbug}       % <--should be always the first line (see CONTRIBUTING)!
\documentclass[11pt]{article}

\usepackage{amsmath,caption}

\DeclareCaptionLabelFormat{uppercase}{\MakeUppercase{#1} #2}
\DeclareCaptionTextFormat{uppercase}{\MakeUppercase{#1}}
\captionsetup[figure]{labelformat=uppercase,textformat=uppercase}

\AddToNoCaseChangeList{ \eqref }

\usepackage{babel} % <== when babel is loaded, \label must be \protect'd. This is a new behavior, after Sept 15, 2022.

\begin{document}
\section{Section one}
\begin{equation}
S = k \ln w \label{eqn:1}
\end{equation}
See eqn.~\eqref{eqn:1} and Fig.~\ref{fig:1}.

\begin{figure}
% The first version does not work:  
%        \label throws errors when it is inside the caption unless it is \protect'd
  \caption{A figure caption, eqn.~\eqref{eqn:1}, $z = y^2$\protect\label{fig:1}}

% If \label is moved out of \caption, everything is fine.
%  \caption{A table caption, eqn.~\eqref{eqn:1}, $z = y^2$}\label{fig:1}  
\end{figure}

\end{document}

Log file (required) and possibly PDF file

caption-label-issue.log

u-fischer commented 1 year ago

A simpler example is

\RequirePackage{latexbug}   
\documentclass{article}

\usepackage{babel}
\begin{document}
\MakeUppercase{abc\label{blub}}
\end{document}

But I think that should be moved to the babel issues, as babel redefines \MakeUppercase.

you can avoid the redefinition by defining \BabelCaseHack:

\def\BabelCaseHack{}
\usepackage{babel}
...
John02139 commented 1 year ago

Thanks, that line fixes my production file.

It appears that the general issue has appeared in babel's github.

I will link this into the babel github, issue latex3/latex2e#189.

jbezos commented 1 year ago

😮 I’ve just pushed a new dev version here unaware of this report. With a quick test, apparently the issue is fixed.

josephwright commented 1 year ago

@jbezos I'm working on the longer-term mechanism here: I hope to have something for the Fall 2022 LaTeX, so code committed in a week or two

jbezos commented 1 year ago

I’m closing this issue for two reasons: (1) it’s basically a duplicate of #189 (well, not quite, but the very closely related); (2) the last commit mostly fixes this particular issue.

I’d like to note the minimal file by Ulrike failed in TeXLive 2021, too, and the label/ref is wrong even if protected. Actually, the new implementation of \MakeUppercase and \MakeLowercase in LaTeX attempts to improve their behavior. However, babel restores partially the old code for the localized casing to work again, which explain why the problem is back.

John02139 commented 1 year ago

Any sense of whether/when a fix for this issue might be posted -- does it require first the work Joseph plans on the kernel? Or will babel continue to not support constructions that place \label inside uppercased captions? (If the latter, is any functionality lost with a patch that uses Ulrike's \def\BabelCaseHack{}? My code lives in a template used by others, & I'm trying to insulate end-users from the details.)

jbezos commented 1 year ago

@John02139 Very likely I’ll submit a new version to CTAN in 7-10 days. No functionality is lost except if you need Medieval Latin or a few other languages.