latex3 / babel

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

Not writing manytime the language switching in the auxiliary files if not required. #108

Closed maieul closed 1 year ago

maieul commented 3 years ago

Oups, sorry, message was send before I write it !

So my problem start here https://github.com/maieul/ledmac/issues/887.

Having a talk with @u-fischer on SE, she pointed the fact that

\documentclass{scrbook}
\usepackage[T1]{fontenc}
\usepackage[german]{babel}
\begin{document}
\tableofcontents

abc\selectlanguage{german} abc \selectlanguage{german}
\end{document}

write three times \german in the auxiliaris files, while it should be easy to remember the actual language, and so not writing anything if not required.

https://chat.stackexchange.com/transcript/message/56242919#56242919

jbezos commented 3 years ago

I was working on a ‘single’ mode for monolingual documents (there are even some parts already written in the current babel), but babel currently works and therefore it was not among my priorities.

u-fischer commented 3 years ago

it is not really about monolingual documents. If there more languages it can happen nevertheless that some code reselects the already active language:

\documentclass{scrbook}
\usepackage[T1]{fontenc}
\usepackage[german,english]{babel}
\begin{document}
\tableofcontents

abc\selectlanguage{german} abc \selectlanguage{german}

abc\selectlanguage{german} abc \selectlanguage{german}

abc\selectlanguage{english} abc 
\end{document}

One could avoid that the language is selected needlessly by using iflang:

\documentclass{scrbook}
\usepackage[T1]{fontenc}
\usepackage[german,english]{babel}
\usepackage{iflang}
\begin{document}
\tableofcontents

abc\IfLanguageName{german}{}{\selectlanguage{german}} abc \IfLanguageName{german}{}{\selectlanguage{german}}

abc\IfLanguageName{german}{}{\selectlanguage{german}} abc \IfLanguageName{german}{}{\selectlanguage{german}}

abc\IfLanguageName{english}{}{\selectlanguage{english}} abc 
\end{document}

But the question is if this is really a user task. Wouldn't it make sense if babel does this?

jbezos commented 3 years ago

Wouldn't it make sense if babel does this?

Indeed, but when considering floats and the like things are not so simple. And this is not the only issue related to a huge quantity of \selectlanguage's because there is an internal macro growing with each selection (\bbl@language@stack). In the following example \IfLanguageName{english}{}{\selectlanguage{english}} is not the same as just \selectlanguage{english}:

\documentclass{article}

\usepackage[english, german]{babel}
\usepackage{iflang}

\begin{document}

\makeatletter

\protected@write\@auxout{}{\string\@gobble{1 \languagename}}%

\selectlanguage{english}

\begin{figure}
\protected@write\@auxout{}{\string\@gobble{F1 \languagename}}%
% \IfLanguageName{english}{}{\selectlanguage{english}}
\selectlanguage{english}
\protected@write\@auxout{}{\string\@gobble{F2 \languagename}}%
\end{figure}

blah

\selectlanguage{english}
\protected@write\@auxout{}{\string\@gobble{2 \languagename}}%

\end{document}

Admittedly, the result with \IfLanguageName perhaps is wrong (a bug?), but anyway it reveals fixing this issue requires a careful analysis.

u-fischer commented 3 years ago

In the following example \IfLanguageName{english}{}{\selectlanguage{english}} is not the same as just \selectlanguage{english}:

well it is not supposed to be same, after all the first should executed the command less often ;-) But which problem do you see? At a first glance it looked ok to me.

jbezos commented 3 years ago

@u-fischer Look at the aux file.

jbezos commented 1 year ago

As the example in https://github.com/latex3/babel/issues/108#issuecomment-733099374 shows, the asynchronous nature of TeX makes impossible to fix this issue in a general way (in the aux file, the lines with \babel@aux can be misplaced if there are floats). I thought there was some hope with luatex, and in fact the callback process_output_buffer can be used to improve things a bit, but just a little bit, so I don’t think it’s a real solution and, anyway, it doesn’t work with pdftex or xetex.

However, in monolingual documents there is a simple solution (≥3.64):

\babeladjust { select.write = omit }

With this setting, no \babel@aux is written to the aux file. There is a minor bug, but currently it should work in most documents.