4

I am trying to set anchor=base for my node labels so that the labels are aligned at their baselines. It seems to work if I set the option in the label itself, but not if I set the option globally.

With no options:

\documentclass[margin=5mm]{standalone} 

\usepackage{tikz}

\begin{document}

\tikzset{mynode/.style={circle,fill=black}}

    \begin{tikzpicture}

        \draw  (0,0) node[mynode,label={Aa}] (n1) {}
            -- (1,0) node[mynode,label={Ag}] (n2) {}
            -- (2,0) node[mynode,label={Bb}] (n3) {};           

    \end{tikzpicture}

\end{document}

enter image description here

With options set in each label:

\documentclass[margin=5mm]{standalone} 

\usepackage{tikz}

\begin{document}

\tikzset{mynode/.style={circle,fill=black}}

    \begin{tikzpicture}

        \draw  (0,0) node[mynode,label={[anchor=base,yshift=2pt]Aa}] (n1) {}
            -- (1,0) node[mynode,label={[anchor=base,yshift=2pt]Ag}] (n2) {}
            -- (2,0) node[mynode,label={[anchor=base,yshift=2pt]Bb}] (n3) {};           

    \end{tikzpicture}

\end{document}

enter image description here

With options set globally:

\documentclass[margin=5mm]{standalone} 

\usepackage{tikz}

\begin{document}

\tikzset{mynode/.style={circle,fill=black}}

    \begin{tikzpicture}[every label/.style={anchor=base,yshift=2pt}]

        \draw  (0,0) node[mynode,label={Aa}] (n1) {}
            -- (1,0) node[mynode,label={Ag}] (n2) {}
            -- (2,0) node[mynode,label={Bb}] (n3) {};           

    \end{tikzpicture}

\end{document}

enter image description here

But setting other options globally seems to work; for example, if I change to

\begin{tikzpicture}[every label/.style={blue,anchor=base,yshift=2pt}]

then I get:

enter image description here

How can I set the alignment globally instead of in each label individually? (Even better would be if I can set the label style within the node style declaration.)

  • You could just use a matrix. – John Kormylo Nov 9 at 22:55
3

Referencing Node baseline in tikz and Section 5.1 Styling the Nodes of the pgf manual:

The problem of mismatching baselines is caused by the fact that . and digit and E all have different heights and depth. If they all had the same, they would all be positioned vertically in the same manner. So, all Ilka needs to do is to use the text height and text depth options to explicitly specify a height and depth for the nodes.

So, if we use, for example

every label/.append style={text depth=.25ex,color=blue,},

then we obtain

enter image description here

Here's a complete example

% arara: pdflatex
\documentclass[margin=5mm]{standalone}

\usepackage{tikz}
\tikzset{mynode/.append style={circle,fill=black},
    every label/.append style={text depth=.25ex,color=blue,},
}
\begin{document}

\begin{tikzpicture}
  \draw  (0,0) node[mynode,label={Aa}] (n1) {}
    -- (1,0) node[mynode,label={Ag}] (n2) {}
    -- (2,0) node[mynode,label={Bb}] (n3) {};

\end{tikzpicture}

\end{document}
2

The problem is that the anchor gets overwritten. In other words, every label gets applied too early. As a workaround, I added a second version of every label, every label add, which gets applied later. If you then say

every label add/.style={anchor=base}

the anchor will be set as desired.

\documentclass[margin=5mm]{standalone} 
\usepackage{tikz}
\makeatletter% from tikz.code.tex
\def\tikz@@parse@label@nonactive[#1]#2:#3:\pgf@nil{%
  \tikzset{%
    append after command = {%
      \bgroup
        [current point is local=true]
        \pgfextra{\let\tikz@save@last@fig@name=\tikz@last@fig@name\tikz@node@is@a@labelfalse}
        node [every label,
              tikz@label@angle = #2,
              anchor=@auto,
              every label add,%<- added
              #1,
              tikz@label@post = \tikz@label@distance] {\iftikz@handle@active@nodes\expandafter\scantokens\else\expandafter\pgfutil@firstofone\fi{#3\noexpand}}
        \pgfextra{\global\let\tikz@last@fig@name=\tikz@save@last@fig@name}
      \egroup}}}%
\tikzset{every label add/.style={}}
\makeatother

\begin{document}

\tikzset{mynode/.style={circle,fill=black}}

    \begin{tikzpicture}

        \draw  (0,0) node[mynode,label={[anchor=base,yshift=2pt]Aa}] (n1) {}
            -- (1,0) node[mynode,label={[anchor=base,yshift=2pt]Ag}] (n2) {}
            -- (2,0) node[mynode,label={[anchor=base,yshift=2pt]Bb}] (n3) {};           

    \end{tikzpicture}


    \begin{tikzpicture}[every
    label/.append style={yshift=2pt},every label add/.style={anchor=base}]

        \draw  (0,0) node[mynode,label={Aa}] (n1) {}
            -- (1,0) node[mynode,label={Ag}] (n2) {}
            -- (2,0) node[mynode,label={Bb}] (n3) {};           

    \end{tikzpicture}

\end{document}

enter image description here

It may conceivably make sense to make a feature request for something of that sort.

  • @LarrySnyder610 As I said, I think you have found a real issue, and debugging it was fun but also some amount of work. IMHO the other answer does not answer the question why the anchor gets ignored at all. – Schrödinger's cat Nov 9 at 15:19
  • That's true, but it's an easier workaround for me, hence the checkmark. However, I do appreciate your debugging, and I will make a feature request to fix this. Do you know what's the best way to go about doing that? – LarrySnyder610 Nov 9 at 15:21
  • @Schrodingerscat Done. – LarrySnyder610 Nov 9 at 15:28

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.