1

I have drawn three axes, or segments of axes, that I want to be aligned at the node (0,0). The first one is drawn, then the second underneath, then the third under that. Two have text above the axes where the text should begin at the node (0,0) but above it.

How can I get them aligned?

When I add the text the axes below shift to the right. How to prevent this so the axes don't move relative to the length or existence of the text?

I am not sure how to plot them all in one tikzpicture, so I have made three to be graphed sequentially.

\begin{tikzpicture}
\draw (0,0) -- (10,0) ; % axis

\foreach \x in {0,2,4,6, 8, 10} % vertical lines
\draw[shift={(\x,0)},color=black] (0pt,3pt) -- (0pt,-3pt);

\node at (0,0) [below=3] {0};
\node at (2,0) [below=3] {1};
\node at (4,0) [below=3] {2};
\node at (6,0) [below=3] {3};
\node at (8,0) [below=3] {4};
\node at (10,0) [below=3] {5};

\end{tikzpicture}

\begin{tikzpicture}
\draw (0,0) -- (8,0) ; % axis

\foreach \x in {0,2,4,6, 8} % vertical lines
\draw[shift={(\x,0)},color=black] (0pt,3pt) -- (0pt,-3pt);

\node at (0,0) [below=3] {0};
\node at (2,0) [below=3] {1};
\node at (4,0) [below=3] {2};
\node at (6,0) [below=3] {3};
\node at (8,0) [below=3] {4};

\node at (0,0) [above=3] {This is the first statement.};

\end{tikzpicture}

\begin{tikzpicture}
\draw (0,0) -- (2,0) ; % axis

\foreach \x in {0,2} % vertical lines
\draw[shift={(\x,0)},color=black] (0pt,3pt) -- (0pt,-3pt);

\node at (0,0) [below=3] {0};
\node at (2,0) [below=3] {1};

\node at (0,0) [above=3] {This is the second statement of a different length.};

\end{tikzpicture}

1 Answer 1

1

I hope I read the question right, if not please let me know. I'd use a pic for the repeating things, and would store the variables in pgf keys. The node alignment can be achieved by setting an appropriate anchor. Then you need only

 \path (0,0)pic{axis} 
  (0,-2) pic{axis={text={This is the first statement.}}}
  (0,-4) pic{axis={text={This is the second statement of a different length.}}};

to get

enter image description here

Full code.

\documentclass[tikz,border=3mm]{standalone}
\begin{document}
\begin{tikzpicture}[line cap=rect,pics/axis/.style={code={
    \tikzset{axis/.cd,#1}%
    \def\pv##1{\pgfkeysvalueof{/tikz/axis/##1}}%    
    \draw (0,0) node[yshift=3pt,anchor=south west]{\pv{text}} 
        -- (\pv{xscale}*\pv{xmax},0)
    foreach \x in {0,...,\pv{xmax}} 
    {(2*\x,3pt) -- ++(0,-3pt) node[below=3]{\x}} ;}},
    axis/.cd,xscale/.initial=2,xmax/.initial=5,text/.initial={}]
 \path (0,0)pic{axis} 
  (0,-2) pic{axis={text={This is the first statement.}}}
  (0,-4) pic{axis={text={This is the second statement of a different length.}}};
\end{tikzpicture}
\end{document}

Here is another example, in which the tick coordinates get names so that one can use them to add an arrow.

\documentclass[tikz,border=3mm]{standalone}
\begin{document}
\begin{tikzpicture}[line cap=rect,pics/axis/.style={code={
    \tikzset{axis/.cd,#1}%
    \def\pv##1{\pgfkeysvalueof{/tikz/axis/##1}}%    
    \draw (0,0) node[yshift=3pt,anchor=south west]{\pv{text}} 
        -- (\pv{xscale}*\pv{xmax},0)
    foreach \x in {0,...,\pv{xmax}} 
    {(2*\x,3pt) -- ++(0,-3pt) node[below=3](-tick-\x){$\x$}} ;}},
    axis/.cd,xscale/.initial=2,xmax/.initial=5,text/.initial={}]
 \path (0,0)pic(a1){axis} 
  (0,-2) pic(a2){axis={text={This is the first statement.},xmax=4}}
  (0,-4) pic(a3){axis={text={This is the second statement of a different
  length.},xmax=1}};
 \draw[-stealth] (a2-tick-1|-0,-2.7) -- (a2-tick-0|-0,-2.7);  
\end{tikzpicture}
\end{document}

enter image description here

8
  • The bottom two axes are supposed to be shorter. The middle one should goes from (0,0) to (8,0) (labeled 0 to 4) and the bottom one from (0,0) to (2,0) (labeled 0 to 1). How would that alter your code (I have not used anchors or pics yet, so I am not entirely clear on all parts of your code)? Also, where is the anchoring done in your code?
    – E2R0NS
    Jun 10, 2020 at 3:19
  • 1
    @E2R0NS There is already a key for that: xmax. Try \path (0,0)pic{axis} (0,-2) pic{axis={text={This is the first statement.},xmax=4}} (0,-4) pic{axis={text={This is the second statement of a different length.},xmax=1}};.
    – user194703
    Jun 10, 2020 at 3:21
  • It works. But sorry to bother, is there a way that I can draw the axis as I did in my original code and then invoke xmax? It's more understandable to me that way.
    – E2R0NS
    Jun 10, 2020 at 3:30
  • 1
    @E2R0NS I do not understand "is there a way that I can draw the axis as I did in my original code and then invoke xmax". And so far the thing does not have an arrow, but one could add one, of course. However, to this end it would be good to have a complete list of options and features the thing one should have. (The good thing about this approach is that one can always "upgrade" the pic by adding more pgf keys without losing downwards compatibility.)
    – user194703
    Jun 10, 2020 at 3:40
  • 2
    @E2R0NS I added an example with an arrow. Whether or not you should ask a separate question also depends on what you want to add. However, it would be good to specify a complete list. The code here is designed to solve the original problem. If the use case is very different from that, it might be that a different approach might be better. One can only pick an approach if one has a more or less complete list of requirements.
    – user194703
    Jun 10, 2020 at 4:13

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .