6

I have a sketch with two aligned lines. These lines have a label each. When you look on my example, you will see, that the labels "Top" and "Inlet" do not share the same baseline. Both labels are placed individually.

How can I define a baseline for both of them?

\begin{tikzpicture}[scale=20]
% Specify the coordinates
\coordinate (P1) at (-0.125,0.15);
\coordinate (P2) at (-0.0635, 0.15);
\coordinate (P3) at (-0.0605, 0.15);
\coordinate (P4) at (0.0, 0.15);

\coordinate (P5) at (-0.125,0);
\coordinate (P6) at (-0.0635,0);
\coordinate (P7) at (-0.0605,0);
\coordinate (P8) at (0.0,0);

\coordinate (P9) at (-0.102588, -0.03);
\coordinate (P10) at (-0.0605, -0.03);
\coordinate (P11) at (0.0, -0.03);

\draw[line width=0.5pt] (P1) -- (P2) -- (P6) -- (P10) -- (P7) -- (P3) -- (P4);
\draw[line width=0.5pt] (P1) -- (P5) -- (P9);
\draw[line width=0.5pt] (P4) -- (P11);
\draw[line width=0.5pt] (P1) -- (P2)  node [midway, above] {Inlet};
\draw[line width=0.5pt] (P3) -- (P4)  node [midway, above] {Top};

\end{tikzpicture}

Image generated by the code.

1
  • {\strut Inlet} and {\strut Top}?
    – egreg
    Commented Jan 11, 2013 at 15:25

3 Answers 3

6

If you want an "automatic" solution that doesn't depend on the labels, then

\draw[line width=0.5pt] (P1) -- (P2)  node [midway, above] {\strut Inlet};
\draw[line width=0.5pt] (P3) -- (P4)  node [midway, above] {\strut Top};

can do.

1
  • 2
    An even more automatic solution would be to issue every node/.style={font=\strut} (globally, to the tikzpicture or to another scope) Commented Jan 11, 2013 at 16:07
6

They are not aligned due to the depth of p in Top. You can use vphantom{p} to align them:

\draw[line width=0.5pt] (P1) -- (P2)  node [midway, above] {Inlet\vphantom{p}};

enter image description here

4
  • 1
    The “opposite” solution would be to add text depth=0ex to the Top node. Commented Jan 11, 2013 at 15:29
  • Does the code "\vphantom{p}" make TikZ align the text "Inlet" in a way, as if the text contained a letter 'p'? And therefore, adjust the spacing between text and line?
    – Dohn Joe
    Commented Jan 11, 2013 at 15:35
  • @DohnJoe: It adds a depth of p but no width.
    – user11232
    Commented Jan 11, 2013 at 15:38
  • @Qrrbrbirlbel That is a valid (and reasonable) solution, consider posting it as an answer!
    – yo'
    Commented Jan 11, 2013 at 16:03
4

(This answer aims at a alignment that uses the original placement of “Inlet” without adding further (vertical) padding to the nodes.)

There is a special anchor .base that would place the at part of the node at the baseline of the text. But this does not help here, as we want to place the node above (equivalent to anchor=south) of the lines. With anchor=base we would get
enter image description here

Though, anchor=south can be mimiced by up-shifting the base-line aligned nodes by the amount of inner ysep; key: base above:
enter image description here

A very similar solution can be obtained, by setting the text depth to zero (and thus ignoring the depth of p); key: Base above:

enter image description here

The actual difference can be seen by drawing the rectangular shape of the node: enter image description hereenter image description here


If the missing depth of p is too tight for you, you can use (as already proposed in the other answers) \vphantom or \strut (which is just a very big \vphantom). But I would rather use a key for this and would not manual insert those macros in the node. We can use the font key for that:

  • ssstrut, Ag:

    enter image description here

  • sstrut, ():

    enter image description here

  • strut, \strut:

    enter image description here

Code

\documentclass[tikz]{standalone}
\tikzset{
%   nodes={draw,draw opacity=.5},% debug
    base above/.style={%                                     shape does not loose its original measurements
        anchor=base, yshift=\pgfkeysvalueof{/pgf/inner ysep}},
    Base above/.style={%                                     “smashing” the bottom part
        above, text depth=0ex},
    ssstrut/.style={%                                        very small strut
        font=\vphantom{Ag}},
    sstrut/.style={%                                         small strut
        font=\vphantom{()}},
    strut/.style={%                                          big strut
        font=\strut}}
\newcommand*{\coordinates}{\coordinate (P1) at (-0.125,0.15) coordinate (P2) at (-0.0635,0.15)
                            coordinate (P3) at (-0.0605,0.15) coordinate (P4) at (0.0,0.15);}
\begin{document}
\begin{tikzpicture}[scale=20] \coordinates
\draw (P1) -- (P2) node [midway, base above] {Inlet};
\draw (P3) -- (P4) node [midway, base above] {Top};
\end{tikzpicture}
\begin{tikzpicture}[scale=20] \coordinates
\draw (P1) -- (P2) node [midway, Base above] {Inlet};
\draw (P3) -- (P4) node [midway, Base above] {Top};
\end{tikzpicture}
\begin{tikzpicture}[scale=20] \coordinates
\draw (P1) -- (P2) node [midway, above, ssstrut] {Inlet};
\draw (P3) -- (P4) node [midway, above, ssstrut] {Top};
\end{tikzpicture}
\begin{tikzpicture}[scale=20] \coordinates
\draw (P1) -- (P2) node [midway, above, sstrut] {Inlet};
\draw (P3) -- (P4) node [midway, above, sstrut] {Top};
\end{tikzpicture}
\begin{tikzpicture}[scale=20] \coordinates
\draw (P1) -- (P2) node [midway, above, strut] {Inlet};
\draw (P3) -- (P4) node [midway, above, strut] {Top};
\end{tikzpicture}
\end{document}

You must log in to answer this question.

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