latex3 / babel

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

Shorthands create issues when creating new environments #282

Closed rlkamalapurkar closed 4 months ago

rlkamalapurkar commented 4 months ago

I asked this question on StackExchange, and a suggestion was to reach out to you to see if shorthands can be protected.

In the bodeplot package, I defined new environments using (defined like this to avoid issues with externalization of tikz graphics)

\documentclass{standalone}
\usepackage{pgfplots,xparse}
\pgfplotsset{compat=1.18}
\NewDocumentEnvironment{MyEnv}{O{}mm+b}{
  \begin{tikzpicture}
    \begin{semilogxaxis}[xmin={#2},xmax={#3},ymin=0,ymax=1]
      #4
    \end{semilogxaxis}
  \end{tikzpicture}
}{}

I want users to be able to use the new environment to do things like this

\begin{document}
  \begin{MyEnv}[]{1e-1}{1e4}
    \draw[thick,blue,dashed] (axis cs:1,0) -- (axis cs:100,1);
    \addplot[variable=t,domain=1e-1:1e4] gnuplot {(t>100)?0.2:0.5};
  \end{MyEnv}
\end{document}

All of this works out for English language documents. However, if a user uses babel with any language that uses some characters in #4 as a shorthand, the document cannot be compiled. For example, if I add

\usepackage[french]{babel}
\usetikzlibrary{babel}

to the preamble, I get compilation errors. The errors persist even if I add \csname @safe@activestrue\endcsname.

I managed to make it work for the French language by checking for frenchbsetup and using

\AddToHook{env/MyEnv/begin}{\shorthandoff{;:!?}}

if it is detected. However, doing something like this for every shorthand in every language seems like the wrong approach to solve this issue.

Is there a better way detect and manage shorthands in macros like this that will work with all languages supported by babel?

jbezos commented 4 months ago

I’ve answered this question in stackexchange, with a guess. I’m not sure how tikz deals with active characters and there isn’t a full MWE. I’m closing this issue, but we can continue in stackexchange.

u-fischer commented 4 months ago

@jbezos And example would be this. It errors because the active chars in {(t>100)?0.2:0.5} are expanded when pgfplots parses the math. And with (crude) protected definitions it doesn't error:

\documentclass{article}
\usepackage{pgfplots,xparse}
\pgfplotsset{compat=1.18}
\NewDocumentEnvironment{MyEnv}{O{}mm+b}{
  \begin{tikzpicture}
    \begin{semilogxaxis}[xmin={#2},xmax={#3},ymin=0,ymax=1]
      #4
    \end{semilogxaxis}
  \end{tikzpicture}
}{}
\usepackage[french]{babel}
\begin{document}
\let\oriq?
\let\oricolon:
\protected\def?{\ifincsname\string?\else\oriq\fi} %comment to see error
\protected\def:{\ifincsname\string:\else\oricolon\fi} %comment to see error

\section{Sec: Sec?}\label{sec:blub}

%\csname @safe@activestrue\endcsname

text: text?

  \begin{MyEnv}[]{1e-1}{1e4}
    \node at (1,0.5) {text?}; 
    \draw[thick,blue,dashed] (axis cs:1,0) -- (axis cs:100,1);
    \addplot[variable=t,domain=1e-1:1e4]  {(t>100)?0.2:0.5};
  \end{MyEnv}
\end{document}
jbezos commented 4 months ago

Related: #196.

rlkamalapurkar commented 4 months ago

@u-fischer the protection you outlined above seems to be incompatible with the babel tikz library. Adding \usetikzlibrary{babel} to your MWE above gives me

! File ended while scanning use of \path.

I can confirm that the "collect all shorthands in a macro and disable them" approach, suggested by @jbezos on StackExchange, works fine with or without the babel library.

I do not know enough about TeX to understand why, but I figured letting you know would help!

jbezos commented 4 months ago

Glad it helped. As I said in stackexchange, a more user friendly way to switch all shorthands on or off can be useful. (Added to my todo list.)