latex3 / babel

The multilingual framework to localize LaTeX, LuaLaTeX and XeLaTeX
https://latex3.github.io/babel/
LaTeX Project Public License v1.3c
130 stars 35 forks source link

Custom alph/Alph equivalents using babelprovide #144

Closed lionslancer closed 2 years ago

lionslancer commented 3 years ago

Currently, \babelprovide can be used to redefine \alph and \Alph, making it possible to enumerate lists with locale-specific numerical systems in the following way:

\babelprovide[alph=katakana.iroha]{japanese}

Babel provides 11 styles for Japanese alone, and since there 4 levels of enumeration, one may want to use locale-specific styles for each level. However, even when redefining both \alph and \Alph, one is still limited to two styles.

Would it be possible to give babel the ability to turn any style into uniquely-named macro? This can already be done by defining a custom macro using the same syntax used in the definition of \alph, but it would be very convenient to be able to leverage babel for this instead of defining a new macro or using a specific package for every style. I think this could potentially work with custom alphabets as well, which would be a simpler syntax for custom enumeration than a custom macro.

jbezos commented 3 years ago

There is a couple of macros for this purpose, used as shown: \localenumeral{katakana.iroha}{1} and \localecounter{katakana.iroha}{section}. The point of alph= and Alph= is to just reconfigure the behavior of these standard counters.

lionslancer commented 3 years ago

Thank you for the fast response!

I am aware of these macros, but I haven't been able to get them to behave as expected in an enumerate environment.

Used with the enumitem package,

\begin{enumerate}[label = \localecounter{katakana.iroha}{enumi}]
    \item This is item \localecounter{katakana.iroha}{enumi}
    \item This is item \localecounter{katakana.iroha}{enumi}
    \item This is item \localecounter{katakana.iroha}{enumi}
\end{enumerate}

simply causes the label to disappear:

image

Meanwhile,

\begin{enumerate}[label = \localenumeral{katakana.iroha}{1}]
    \item This is item \localenumeral{katakana.iroha}{1}
    \item This is item \localenumeral{katakana.iroha}{1}
    \item This is item \localenumeral{katakana.iroha}{1}
\end{enumerate}

Works, but obviously doesn't continue the enumeration. image

Interestingly, \localenumeral works just fine when not used as a label. I don't think enumitem is at fault, because I haven't gotten it to work by redefining \theenumi and \labelenumi either. I have a feeling \localecounter is called before the counter steps up, causing the breakage in the labeling, but I am far from certain.

jbezos commented 3 years ago

Oh, well, an XY problem 🙂. Just protect it:

\begin{enumerate}[label = \protect\localecounter{katakana.iroha}{enumi}]
    \item This is item \localecounter{katakana.iroha}{enumi}
    \item This is item \localecounter{katakana.iroha}{enumi}
    \item This is item \localecounter{katakana.iroha}{enumi}
\end{enumerate}

The warning in the enumitem manual about the counters (sec. 3.1) is incomplete (and even a bit misleading) because it assumes the default counters (enumitem does some tricky things with them, which is the very reason \AddEnumerateCounter exists). Note, however, enumitem can’t compute the width with * values except if the latter command is used. But this is unrelated to babel.

lionslancer commented 3 years ago

Oh, well, an XY problem 🙂.

Apologies. I've been trying to troubleshoot this for a few days and I guess I ended up with tunnel vision. I'll try to be more careful about not asking about the wrong thing in the future.

Just protect it

That fixed it :)

I experimented a bit and found something strange, though. The following document compiles just fine and comes out as I'd like it, but only if cjk-heavenly-stem and cjk-earthly-branch are marked as Chinese or Korean. Am I doing something wrong or are these styles broken for Japanese?

\documentclass[12pt, a4paper, article, luatex]{jlreq}
\usepackage[japanese, provide=*]{babel}
\usepackage{enumitem}

\setlist[enumerate,1]{label = \protect\localecounter{katakana.iroha}{enumi}\space}
\setlist[enumerate,2]{label = \protect\localecounter{circled.katakana}{enumii}\space}
\setlist[enumerate,3]{label = \protect\localecounter{cjk-heavenly-stem}{enumiii}\space}
\setlist[enumerate,4]{label = \protect\localecounter{cjk-earthly-branch}{enumiv}\space}

\begin{document}

\begin{enumerate}
    \item Level 1
    \item Level 1
    \begin{enumerate}
        \item Level 2
        \item Level 2
        \begin{enumerate}
            \item Level 3
            \item Level 3
            \begin{enumerate}
                \item Level 4
                \item Level 4
            \end{enumerate}
        \end{enumerate}
    \end{enumerate}
\end{enumerate}

\end{document}

image image

The right image being the result when the following is used:

\setlist[enumerate,3]{label = \foreignlanguage{chinese}{\protect\localecounter{cjk-heavenly-stem}{enumiii}}\space}
\setlist[enumerate,4]{label = \foreignlanguage{korean}{\protect\localecounter{cjk-earthly-branch}{enumiv}}\space}
jbezos commented 3 years ago

There is a mistake in the documentation, because these two counters aren’t defined for Japanese (basically because in CSS they aren't, see Ready-made Counter Styles). But if they are used, as a note in that page implies, I can add them.

lionslancer commented 3 years ago

I see. Looking at the that page, I definitely get the impression that all styles currently defined for Chinese should be considered pan-CJK, and that the Japanese and Korean locales ought to be thought of as supersets including their respective language-specific styles. That's most likely why the section is labeled "Han & CJK" rather than "Chinese". It doesn't seem like there are any Chinese-specific styles currently defined.

As for whether these particular styles are used in practice, I know they were commonly used historically, but I'm not qualified to comment on current usage. Perhaps this discussion can be of use.

jbezos commented 2 years ago

I’m closing this issue because it was partly a problem in how enumitem was set, and partly in a few missing babel counters which has been already added (see https://latex3.github.io/babel/news/whats-new-in-babel-3.64.html).