0

The explicit code

\documentclass{standalone}
\usepackage[french]{babel}
\usepackage{pgfplots}
\pgfplotsset{compat=1.18}
\usetikzlibrary{babel}
\begin{document}
\begin{tikzpicture}
  \begin{semilogxaxis}[
    xmin=1e-1,
    xmax=1e4,
    ymin=0,
    ymax=1
  ]
    \draw[thick,blue,dashed] (axis cs:1,0) -- (axis cs:100,1);
  \end{semilogxaxis}
\end{tikzpicture}
\end{document}

works but if I use a new environment, it fails to compile

\documentclass{standalone}
\usepackage{pgfplots,environ}
\usepackage[french]{babel}
\usetikzlibrary{babel}
\pgfplotsset{compat=1.18}
\NewEnviron{MyEnv}[3][]{
  \begin{tikzpicture}
    \begin{semilogxaxis}[
      xmin={#2},
      xmax={#3},
      ymin=0,
      ymax=1
    ]
      \BODY
    \end{semilogxaxis}
  \end{tikzpicture}
}
\begin{document}
\begin{MyEnv}[]{1e-1}{1e4}
    \draw[thick,blue,dashed] (axis cs:1,0) -- (axis cs:100,1);
\end{MyEnv}
\end{document}

The second example compiles correctly if I comment out the babel package.

Any ideas on how to fix this? The shorthandoff suggestions in answers to other similar questions do not seem to work here.

9
  • 2
    why are you grabbing the body instead of using a normal environment? Commented Jan 10 at 22:55
  • I wrote the bodeplot package a while ago, and at the time, I could not figure out how to handle the argument macros correctly using the usual \newenvironment command. I was able to get it to work using \newenviron from the environ package. The functionality I wanted was the ability to pass tikz commands and pgf keys to environments inside the newly created environment. For example, write something like \begin{MyEnv}[tikz/{},axes/{}]... and pass the tikz options to tikzpicture and axes options to axis. I got that to work using \newenviron.
    – Rushi
    Commented Jan 10 at 23:24
  • sorry but I can't see what you gain from environ. It makes things only more complicated here as it freezes catcodes. Commented Jan 10 at 23:38
  • There might very well be a way to do what I need without environ, unfortunately, I am better at control theory than LaTeX :-) I'll try to rewrite my package again without environ, but just wanted to see if anyone has any quick fixes in the meantime...
    – Rushi
    Commented Jan 11 at 3:01
  • 1
    You can disable the shorthand with \AddToHook{env/MyEnv/begin}{\shorthandoff{;}}. Btw: the environ package is no longer needed, a current LaTeX has the functionality in-built. \NewDocumentEnvironment{MyEnv}{omm+b}{...}{}. Use #4 instead of \BODY in the content. Commented Jan 11 at 9:12

1 Answer 1

1

EDIT: A better way to do this is to generate a list of current shorthands and then disable all of them, see this answer. Manually disabling shorthands like ; can lead to errors if ; is not a shorthand (see comments below).


Original answer: As suggested by @UlrikeFischer in the comments,

\AddToHook{env/MyEnv/begin}{\shorthandoff{;}}

to handle active characters works. The environ package was used to avoid conflicts with the tikz external library. The xparse package can also be used instead of environ.

\documentclass{standalone}
\usepackage[french]{babel}
\usepackage{xparse}
\usetikzlibrary{babel,external}
\tikzexternalize[prefix=figure]
\pgfplotsset{compat=1.18}
\AddToHook{env/MyEnv/begin}{\shorthandoff{;}}
\NewDocumentEnvironment{MyEnv}{O{}mm+b}{
    \begin{tikzpicture}
        \begin{semilogxaxis}[xmin={#2},xmax={#3},ymin=0,ymax=1]
            #4
        \end{semilogxaxis}
    \end{tikzpicture}
}{}
\begin{document}
    \begin{MyEnv}[]{1e-1}{1e4}
        \draw[thick,blue,dashed] (axis cs:1,0) -- (axis cs:100,1);
    \end{MyEnv}
\end{document}
7
  • 1
    In a multilingual document with French and e.g English, and English being the main language, bodeplot will try to switch handles off: \shorthandoff{;:!?}, even though there are not active. The test \AtBeginDocument{ \ifdefined\frenchbsetup only works in a monolingual French document
    – ebcontrol
    Commented Jan 30 at 11:17
  • 1
    Darn. I'll see if I can check whether the characters are active directly, instead of checking whether French language is selected. Does anyone have any other ways this can be achieved?
    – Rushi
    Commented Jan 31 at 14:19
  • 1
    based on one of your examples: works fine with pdflatex, error with lualalatex, had not realised this before "! Package babel Error: I can't switch '?' on or off--not a shorthand." \documentclass{article} \usepackage[french,main=english]{babel} \usepackage[pgf]{bodeplot} \begin{document} \begin{BodeMagPlot}[height=2cm,width=4cm] {0.01} {100} \addBodeZPKPlots[% true/{black,thick}, linear/{red,dashed,thick}, asymptotic/{blue,dotted,thick}] {magnitude} {z/{0,{-0.1,-0.5},{-0.1,0.5}},p/{{-0.5,-10},{-0.5,10}},k/10} \end{BodeMagPlot} \end{document}
    – ebcontrol
    Commented Feb 9 at 7:39
  • 1
    I fixed this thanks to this answer by Javier Bezos, the babel maintainer. Version 1.1.7 should work regardless of languages or compilers. I tested your example, and it does fail with lualatex in v1.1.6, but compiles fine in v1.1.7.
    – Rushi
    Commented Feb 11 at 1:40
  • 1
    thanks to the changes, with Version 1.1.7 also my large document compiles error-free
    – ebcontrol
    Commented Feb 12 at 4:38

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .