I have macros that use anchor points in pgfplots to access certain coordinate. For instance, in a graph I can invoke a macro such as:
\newcommand*{\DoSomethingWithXAxis}{%
\draw [ultra thick, blue] (current axis.left of origin) -- (current axis.right of origin);
}
I would like to be able to reuse such macros in a basic tikzpicture environment by manually defining the coordinates (current axis.left of origin) and (current axis.right of origin). The problem stems from the fact that those are anchor points, so was wondering what is the simplest approach to set those.
Notes:
- I realize that
\DoSomethingWithXAxispresented here is not very interesting and that there are easier ways to accomplish the same goal. But, this is just a MWE, with emphasis on theMpart of theMWE.
The MWE below has \DoSomethingWithXAxis commented out in the first tikzpicture and yields the following. The desired result is for the tikzpicture axis to also be in blue via \DoSomethingWithXAxis}.
References:
Code:
\documentclass{article}
\usepackage{pgfplots}
\newcommand*{\DoSomethingWithXAxis}{%
\draw [ultra thick, blue] (current axis.left of origin) -- (current axis.right of origin);
}
%% Only specifying one side to ensure that this works with both
%% manually specified end point and an auto determined one.
\newcommand*{\XMax}{8}
\begin{document}
\begin{tikzpicture}
\draw [-latex] (1,0) -- (\XMax,0) node [above, pos=0.5] {tikzpicture};
%% Don't think these coordinates are being set correctly?
\coordinate (current axis.left of origin) at (1,0);
\coordinate (current axis.right of origin) at (\XMax,0);
%\DoSomethingWithXAxis;%%% <---- How do this ????
\end{tikzpicture}
\par\medskip
\begin{tikzpicture}
\begin{axis}[axis lines=middle, samples=50, xmax=\XMax]
\addplot [mark=none, red, thick] {x*x} node [pos=0.75, right] {pgfplots};
\DoSomethingWithXAxis;
\end{axis}
\end{tikzpicture}
\end{document}


left of originetc. are anchors of the shapepgfplots@low@level@shapebut to set them you need to be inside an axis because otherwise the macros that set them do not have all necessary information. Do you want to create another shape that allows you to set these anchors as you want, i.e. by hand? – Jul 11 '18 at 19:54pgfto create shapes. I want to be able to define the point that is meant by(current axis.right of origin)outside of anaxisenvironment. Desire is to not need two versions of each of those macros: one that works inside anaxisenvironment and one that works in atikzpicture(outside of anaxisenvironment). – Peter Grill Jul 11 '18 at 21:09