1

Can you explain why the node labelled with s is misaligned on the following tikzpicture?

\documentclass[tikz]{standalone}

\begin{document}
    \begin{tikzpicture}
\node at (0,0) {d};
\node at (0.5,0) {s};
    \end{tikzpicture}
\end{document}

enter image description here

2 Answers 2

3

By default tikz aligns the nodes in the center of the boxes that surround them.

anchor-default

\documentclass[tikz,border=5mm]{standalone}

\begin{document}
    \begin{tikzpicture}
\node[draw,anchor=center] at (0,0) {d};
\node[draw,anchor=center] at (0.5,0) {s};
    \end{tikzpicture}
\end{document}

To align the nodes on their base, simply choose the node base as the anchor.

anchor-base

\documentclass[tikz,border=5mm]{standalone}

\begin{document}
    \begin{tikzpicture}
\node[draw,anchor=base] at (0,0) {d};
\node[draw,anchor=base] at (0.5,0) {s};
    \end{tikzpicture}
\end{document}

anchor-base-no-draw

\documentclass[tikz,border=5mm]{standalone}

\begin{document}
    \begin{tikzpicture}
\node[anchor=base] at (0,0) {d};
\node[anchor=base] at (0.5,0) {s};
    \end{tikzpicture}
\end{document}

See page 227 of the manual.

Supplements in response to @bmv's question

The height of the text in their box is adjusted with the text height parameter.

text-height

\documentclass[border=5mm]{standalone}
\usepackage{tikz}
\begin{document}
    \begin{tikzpicture}
\node[draw,anchor=base,text height=0pt] at (0,0) {d};
\node[draw,anchor=base,text height=0pt] at (0.5,0) {s};
    \end{tikzpicture}

     \begin{tikzpicture}
\node[draw,anchor=base,text height=5pt] at (0,0) {d};
\node[draw,anchor=base,text height=5pt] at (0.5,0) {s};
    \end{tikzpicture}   

        \begin{tikzpicture}
\node[draw,anchor=base,text height=10pt] at (0,0) {d};
\node[draw,anchor=base,text height=10pt] at (0.5,0) {s};
    \end{tikzpicture}
\end{document}
2
  • A follow-up question (I'm not sure to open a new thread): how to achieve the boxes to be of the same height?
    – bmv
    Oct 6, 2018 at 15:35
  • @bmv I edited the answer to answer you
    – AndréC
    Oct 6, 2018 at 17:08
2

an alternative solution:

\documentclass[tikz, margin=3.141592mm]{standalone}

\begin{document}
    \begin{tikzpicture}[box/.style={draw, text height=1.5ex, text depth=0.5ex}]
\node[box] at (0,0)     {d};
\node[box] at (0.5,0)   {s};
\node[box] at (1.0,0)   {p};
\node[box] at (1.5,0)   {g};
    \end{tikzpicture}
\end{document}

enter image description here

You must log in to answer this question.

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