4

I try to get the values of line widths and node inner sep. How to read such a value form own style (if it will be specified during modifications of the picture) or if it is not specified in this style, how to get the value applied by drawing line or node?

Code:

\documentclass{standalone}
\usepackage{tikz}

\begin{document}
\begin{tikzpicture}[
    every node/.style={line width=2pt},
    my line style/.style={very thick},
    my node style1/.style={draw,inner sep=1em},
    my node style2/.style={draw}
]
    \pgfkeys{/pgf/inner xsep/.get=\innerxsep}
    \node at (0,0) [my node style2,anchor=west] {x inner sep = \innerxsep};

    %\pgfkeys{ ????  /.get=\innerxsep} % how to read inner sep value applied while "my node style1" is used,
                                       % independently on if it is specified in this style?
                                       % how to read border width value applied while "my node stylei" is used,
                                       % independently on if it is specified in this style?
    \node at (0,-1) [my node style1,anchor=west] {x inner sep = ?, border width = ?};

    %\pgfkeys{ ????  /.get=\lw} % how to read line width value applied while "my line style" is used,
                                % independently on if it is specified in this style?
    \draw [my line style] (0,-2) -- +(1,0) node [right] {line width=?};
\end{tikzpicture}
\end{document}

I tried follow answers given by @Loop Space (Reading - and effect - of (default) tikz/pgf keys?) and by @Andrew Swann (Check the value of a pgfkey), but without success in my problem.

7

Your own style doesn't change any intrinsic properties of the line width and inner sep settings. So you can still use the regular key setting/reading ways.

\documentclass[tikz]{standalone}
\begin{document}
\begin{tikzpicture}[
    every node/.style={line width=2pt},
    my line style/.style={very thick},
    my node style1/.style={draw,inner sep=1em},
    my node style2/.style={draw}
]
    \node at (0,0) [my node style2,anchor=west] 
         {x inner sep = \pgfkeysvalueof{/pgf/inner xsep}};
    \node at (0,-1) [my node style1,anchor=west] 
         {x inner sep = \pgfkeysvalueof{/pgf/inner xsep}, border width = \the\pgflinewidth};
    \draw [my line style] (0,-2) -- +(1,0) node [right] {line width=\the\pgflinewidth};
\end{tikzpicture}
\end{document}

enter image description here

  • Thx. So after the call of some custom style in square brackets the following values can be received from "/pgf/inner sep" and "\the\pgflinewidth"? – forrest Mar 29 '15 at 17:08
  • @forrest I think I didn't understand your question. Every key sets a value in a few different ways. Line width forexample modifies \pgflinewidth parameter where as inner xsep holds the value in a key. If you know how it saves the value you can retrieve it. But not all keys save the value some directly processes the argument there is no way to recover easily. So each is a special case. – percusse Mar 29 '15 at 17:32
  • I consider a situation in which the values of "inner sep" and "line width" were not set up in the heading (so the default values 0.3333em and 0.4pt are used) and the elements of the picture are aligned correctly for these values. And making after some time changes of these values in my own styles will not cause that elements of the picture loss mutual alignment. As I understand, I can't avoid specification of all values that are crucial for proper connection of distinct elements of the picture (?). Here is the same question: tex.stackexchange.com/questions/228781/arrow-box-aligment – forrest Mar 29 '15 at 18:08
  • @forrest Ah OK. I think I'm starting understand. So you would like to record some values of a particular node and then use it in other styles. Is that a little closer? If you replace the 0.3333em in your first commented computation with \pgfkeysvalueof{/pgf/inner xsep}) the nodes look aligned. – percusse Mar 29 '15 at 18:39
  • Yes - that is true. If I define in the heading a new parameter, like "my inner sep", and I apply this parameter both in my own style definition as well in computations for elements alignment, everything is OK. But if no such a parameter I will define, and after some time I will decide to apply some customized "inner sep" in my own style definition, then the alignment of picture elements will be lost. So I am looking for the way of protection the picture against the lost of elements alignment caused by the further changes in style definition. The goal is to build associative pictures. – forrest Mar 29 '15 at 19:27

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.