Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
seloumi opened this issue Oct 28, 2024 · 4 comments
Open

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

seloumi opened this issue Oct 28, 2024 · 4 comments

Comments

@seloumi
Copy link
Contributor

seloumi commented Oct 28, 2024

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
Copy link
Contributor Author

seloumi commented Nov 1, 2024

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}

@jbezos
Copy link
Contributor

jbezos commented Nov 14, 2024

I haven't forgotten about this issue. Sadly, if I remove that line other things break. I’m investigating what’s going on exactly.

@seloumi
Copy link
Contributor Author

seloumi commented Nov 20, 2024

From pgfplots.code.tex command \pgfplotsutil@BEGIN@cell@picture contain \pgfinterruptpicture which causes the problem, here a solution for the problem:

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

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

\makeatletter
\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
      \ifx\pgfplotsset\@undefined\else
        \bbl@sreplace\pgfplotsutil@BEGIN@cell@picture{\tikzpicture}{\bbl@pictsetdir\tw@\tikzpicture}%
      \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}

@seloumi
Copy link
Contributor Author

seloumi commented Nov 20, 2024

Or maybe just like that

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

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

\makeatletter
\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@}%
        \bbl@sreplace\tikzpicture{\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}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants