latex3 / babel

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

Issue with axis environment in RTL context (graphics layout) #318

Open seloumi opened 4 days ago

seloumi commented 4 days ago

This example of plotting using pgfplots in a right-to-left document (lualatex engine) give wrong result, the layout=graphics option is supposed to place tikzpicture environment in LTR context but after adding axis environment the graph exits the page.

https://tex.stackexchange.com/q/729573/54817

\documentclass[12pt]{article}
\usepackage[bidi=basic,layout=graphics]{babel}
\usepackage{pgfplots}

\babelprovide[import,main]{arabic}
\pgfplotsset{compat=1.18}
\pagestyle{empty}

\begin{document}

\begin{tikzpicture}
\begin{axis}[
x=1cm,y=1cm,
axis lines=middle,
ymajorgrids=true,
xmajorgrids=true,
xmin=-6.2,
xmax=6.2,
ymin=-8.2,
ymax=4.2,
xtick={-6,-5,...,6},
ytick={-8,-7,...,4},]
\clip(-6.2,-8.64) rectangle (6.2,4.08);
\addplot[smooth,domain=-4:4,purple,very thick] {x^2-3};
\end{axis}
\end{tikzpicture}

\end{document}
seloumi commented 23 hours ago

It seems that this line of code \bbl@add\pgfinterruptpicture{\bbl@pictresetdir} is what is causing the problem.

\documentclass[12pt]{article}
\usepackage[bidi=basic]{babel}
\usepackage{pgfplots}

\babelprovide[import,main]{arabic}
\pgfplotsset{compat=1.18}
\pagestyle{empty}

\let\bbl@pictresetdir\relax
   \def\bbl@pictsetdir#1{%
     \ifcase\bbl@thetextdir
       \let\bbl@pictresetdir\relax
     \else
       \ifcase#1\bodydir TLT  % Remember this sets the inner boxes
         \or\textdir TLT
         \else\bodydir TLT \textdir TLT
       \fi
       % \(text|par)dir required in pgf:
       \def\bbl@pictresetdir{\bodydir TRT\pardir TRT\textdir TRT\relax}%
     \fi}%
   \AtBeginDocument
     { \ifx\pgfpicture\@undefined\else % TODO. Allow deactivate?
        \AddToHook{env/pgfpicture/begin}{\bbl@pictsetdir\@ne}%
 % ---> \bbl@add\pgfinterruptpicture{\bbl@pictresetdir}%
        \bbl@add\pgfsys@beginpicture{\bbl@pictsetdir\z@}%
      \fi
      \ifx\tikzpicture\@undefined\else
        \AddToHook{env/tikzpicture/begin}{\bbl@pictsetdir\tw@}%
        \bbl@add\tikz@atbegin@node{\bbl@pictresetdir}%
        \bbl@sreplace\tikz{\begingroup}{\begingroup\bbl@pictsetdir\tw@}%
      \fi
      }
\makeatother

\begin{document}

\begin{tikzpicture}
\begin{axis}[
x=1cm,y=1cm,
axis lines=middle,
ymajorgrids=true,
xmajorgrids=true,
xmin=-6.2,
xmax=6.2,
ymin=-8.2,
ymax=4.2,
xtick={-6,-5,...,6},
ytick={-8,-7,...,4},]
\clip(-6.2,-8.64) rectangle (6.2,4.08);
\addplot[smooth,domain=-4:4,purple,very thick] {x^2-3};
\end{axis}
\end{tikzpicture}

\end{document}