2

I have determined the x-coordinate of a point (A) and printed it next to A via

\node[below left] at (A) {%
  \pgfgetlastxy{\macrox}{\macroy}
  \transformxdimension{\macrox}
  \pgfmathprintnumber{\pgfmathresult}};

Now I would like to further use \pgfmathresult and store the value in let's say a variable (or to write it to a file)

\xdef\xA{\pgfmathresult}

which however does not hold original value anymore. Is there a way to still use \pgfmathresult determined previously?

Thanks a lot,

Cluny


Attached, a mini-example, which further illustrates the idea.

\documentclass[tikz]{standalone}

\usepackage{pgfplots}

\pgfplotsset{
    compat=1.5, 
    width=10cm,
}

\usetikzlibrary{intersections}

\tikzset{
  crossp/.style={
    thick,
    draw=gray,
  },
}

%----------------------------------------------------------------------------------

\begin{document}

  \makeatletter
  \newcommand\transformxdimension[1]{
    \pgfmathparse{((#1/\pgfplots@x@veclength)+\pgfplots@data@scale@trafo@SHIFT@x)/10^\pgfplots@data@scale@trafo@EXPONENT@x}
  }
  \makeatother


\begin{tikzpicture}[baseline]

\begin{axis}[
    %small,
    x=0.5cm, y=0.5cm,
    ymajorgrids, xmajorgrids,
    ymin=0, ymax=10,    
    xmin=0, xmax=10,    
]   


\draw[name path global=lineA] (axis cs:0,0) -- (axis cs:10,10);
\draw[name path global=lineB] (axis cs:0,8) -- (axis cs:10,1);

\path[name intersections={of=lineA and lineB, by=A}];
\node[fill=red, circle, inner sep=1.5pt] at (A) {};

\node[left] at (A) {%
  \pgfgetlastxy{\macrox}{\macroy}
  \transformxdimension{\macrox}
  \pgfmathprintnumber{\pgfmathresult}};

%\pgfmathsetmacro{\xA}{\pgfmathresult}

\end{axis}


\end{tikzpicture}
\end{document}

enter image description here

  • Welcome to TeX.SX! Please help us help you by providing a minimal working example (documentclass, needed packages, document environment). – TeXnician Jul 18 '17 at 15:23
  • Can't you simply use \pgfmathsetmacro{\xA}{<your-computation>} ? – marsupilam Jul 18 '17 at 15:29
  • @marsupilam: Unfortunately, redo the calculation and trying e.g., \pgfmathsetmacro{\xA}{\pgfgetlastxy{\macrox}{\macroy}\transformxdimension{\macrox}\pgfmathresult} results in an error. – Cluny Jul 18 '17 at 15:55
  • TikZ uses \pgfmathresult internally for almost everything, so you need to copy it (\global\let\myresult=\pgfmathresult) immediately. – John Kormylo Jul 18 '17 at 15:58
  • @JohnKormylo That is almost what I was looking for. However, as soon as I try to put this variable to a file \immediate\write\tempfile{\myresult}, it states an undefined control sequence. With other variables I don't see this issue. – Cluny Jul 18 '17 at 16:12
2

You have to save the result before the statement ends.

\documentclass[tikz]{standalone}

\usepackage{pgfplots}

\pgfplotsset{
    compat=1.5, 
    width=10cm,
}

\usetikzlibrary{intersections}

\tikzset{
  crossp/.style={
    thick,
    draw=gray,
  },
}

%----------------------------------------------------------------------------------

\begin{document}

\makeatletter
\newcommand\transformxdimension[1]{%
  \pgfmathparse{%
    ((#1/\pgfplots@x@veclength)+\pgfplots@data@scale@trafo@SHIFT@x)/%
    10^\pgfplots@data@scale@trafo@EXPONENT@x
  }
}
\makeatother


\begin{tikzpicture}[baseline]

\begin{axis}[
    %small,
    x=0.5cm, y=0.5cm,
    ymajorgrids, xmajorgrids,
    ymin=0, ymax=10,    
    xmin=0, xmax=10,    
]   


\draw[name path global=lineA] (axis cs:0,0) -- (axis cs:10,10);
\draw[name path global=lineB] (axis cs:0,8) -- (axis cs:10,1);

\path[name intersections={of=lineA and lineB, by=A}];
\node[fill=red, circle, inner sep=1.5pt] at (A) {};

\node[left] at (A) {%
  \pgfgetlastxy{\macrox}{\macroy}%
  \transformxdimension{\macrox}%
  \pgfmathprintnumber{\pgfmathresult}%
  \xdef\xA{\pgfmathresult}%
};

\end{axis}


\end{tikzpicture}

\show\xA

\end{document}

With this I get

> \xA=macro:
->4.71.

on the terminal.

  • Thank you, however, also in this case I am somehow not able to further use the variable as an operand. – Cluny Jul 18 '17 at 17:08
  • @Cluny In what sense? The macro will be available for whatever purpose. – egreg Jul 18 '17 at 17:09
  • For instance,\pgfmathsetmacro{\xAIncrement}{\xA + 1} afterwards fails. It seems that \xA is kind of a wrong data type. – Cluny Jul 18 '17 at 17:12
  • @Cluny If I do \pgfmathsetmacro{\xAIncrement}{\xA + 1}, then \show\xAIncrement yields 5.71. – egreg Jul 18 '17 at 19:44
  • I see why it was failing. I tried to process \xA within the {axis} environment, but there \xA is somehow not defined. Anyway, operating on the variable outside of {axis} is sufficient. Thanks a lot! – Cluny Jul 19 '17 at 8:57

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.