2

I observed the following behavior when plotting a simple axis-diagram in a tikz + beamer document depending on whether 'remember picture,overlay' is used or not.

The following code

\documentclass{beamer}

\usepackage{tikz}
\usepackage{pgfplots}
\usetikzlibrary{positioning}

\begin{document}
\begin{frame}

\begin{tikzpicture}



\begin{axis}[
at={(current page.center)},
height = 12em, width = 12em,
axis lines = middle,
enlargelimits = true,
xmin = 0, xmax = 2, ymin = 0, ymax = 2,
xlabel = {$X_1$},
ylabel = {$X_2$},
x label style={at={(current axis.right of origin)},anchor=north},
y label style={at={(current axis.above origin)},anchor=east},
]

\draw[dashed,gray] (axis cs:1,0) -- (axis cs:1,2);
\draw[dashed,gray] (axis cs:0,1) -- (axis cs:1,1);

\end{axis}

\end{tikzpicture}
\end{frame}
\end{document}

gives correct axis-label positioning

However, when using 'remember picture,overlay' the positioning of the axis labels gets lost. The code:

\documentclass{beamer}

\usepackage{tikz}
\usepackage{pgfplots}
\usetikzlibrary{positioning}

\begin{document}
\begin{frame}

\begin{tikzpicture}[remember picture,overlay]



\begin{axis}[
at={(current page.center)},
height = 12em, width = 12em,
axis lines = middle,
enlargelimits = true,
xmin = 0, xmax = 2, ymin = 0, ymax = 2,
xlabel = {$X_1$},
ylabel = {$X_2$},
x label style={at={(current axis.right of origin)},anchor=north},
y label style={at={(current axis.above origin)},anchor=east},
]

\draw[dashed,gray] (axis cs:1,0) -- (axis cs:1,2);
\draw[dashed,gray] (axis cs:0,1) -- (axis cs:1,1);

\end{axis}

\end{tikzpicture}
\end{frame}
\end{document}

it produces: incorrect placement

Note, the the axis-positioning on the frame is not the problem (I want to put it relative to the center of the frame), but I think that the axis labels should stick to the axis. At least this what I would like to obtain.

Any help is welcome! Thank you in advance!

1

You can use relative coordinates to place the axis labels correctly.

x label style={at={(1,0)},anchor=north},
y label style={at={(0,1)},anchor=east},

enter image description here

\documentclass{beamer}
\usepackage{tikz}
\usepackage{pgfplots}
\usetikzlibrary{positioning}

\begin{document}
\begin{frame}

\begin{tikzpicture}[remember picture,overlay]
\begin{axis}[at={(current page.center)},
height = 12em, width = 12em,
axis lines = middle,
enlargelimits = true,
xmin = 0, xmax = 2, ymin = 0, ymax = 2,
xlabel = {$X_1$}, ylabel = {$X_2$},
x label style={at={(1,0)},anchor=north},
y label style={at={(0,1)},anchor=east},
]

\draw[dashed,gray] (1,0) -- (1,2);
\draw[dashed,gray] (0,1) -- (1,1);

\end{axis}
\end{tikzpicture}
\end{frame}
\end{document}
  • Thank you very much, indeed! This is one way to do it, although I don't understand why the (current axis.right of origin) syntax is not working. Seems to me that current axis is not moved by virtue of remember picture,overlay. – Thomas Hilger Oct 30 '19 at 10:14
  • @ThomasHilger You won't have that problem if you remove at={(current page.center)},. – nidhin Oct 30 '19 at 10:26
  • Sure, but then I wuold have to take different measures for my placement. – Thomas Hilger Nov 5 '19 at 17:04

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.