3

When putting a tikzpicture environment inside an itemize environment, the bullet point is not correctly aligned with the tikz picture. In this case, "correct" means that the bullet should be vertically centered with respect to the picture.

I've previously just tweaked the baseline manually to get what I want, but the tweak will depend on the font I'm using, etc. I'd like to avoid the use of a magic number in my baseline tweak, so that it will adjust automatically to the font I'm using. Any suggestions how to accomplish that?

\documentclass{article}
\usepackage{tikz}
\begin{document}

\begin{itemize}
    \item
        \begin{tikzpicture}%[baseline = -2.5pt] % magic number tweak
            \draw [very thick, ->] (0,0) -- (2,0);
            \node[left] at (0,0) {XX};
            \node[right] at (2,0) {YY};
        \end{tikzpicture}
\end{itemize}

\end{document}

enter image description here

Sverre
  • 20,729
  • 1
    No magic number: -0.5ex will work in the majority of the cases. – Claudio Fiandrino Sep 30 '14 at 12:58
  • @ClaudioFiandrino Not in the document I'm writing now. With -0.5ex, the bullet aligns with the upper half of a x-height character rather than being centered with it. – Sverre Sep 30 '14 at 13:01

1 Answers1

6

You are better off aligning to a known natural point in a node:

Sample output

\documentclass{article}
\usepackage{tikz}
\begin{document}

\begin{itemize}
\item
  \begin{tikzpicture}[baseline=(A.base)] % magic number tweak
    \draw [very thick, ->] (0,0) -- (2,0);
    \node[left] at (0,0) (A) {XX};
    \node[right] at (2,0) {YY};
  \end{tikzpicture}
\item \( X \)
\end{itemize}

\end{document}

If you want to fix the horizontal alignment (which you didn't ask about but Barbara Beeton did) then you will have to work a little harder.

Sample with horizontal spacing

\documentclass{article}

\usepackage{tikz}
\usetikzlibrary{calc}

\begin{document}

\begin{itemize}
\item
  \begin{tikzpicture}[baseline=(A.base)] % magic number tweak
    \node at (0,0) (A) {XX};
    \pgfkeysgetvalue{/pgf/inner xsep}{\myAxsep}
    \node at (2,0) (B) {YY};
    \pgfkeysgetvalue{/pgf/inner xsep}{\myBxsep}
    \draw [very thick, ->] (A) -- (B);
    \pgfresetboundingbox
    \useasboundingbox ($(A.south west)+(\myAxsep,0)$) rectangle
    ($(B.north east)-(\myBxsep,0)$);
  \end{tikzpicture}
  and some text.
\item \( X \)
\end{itemize}

\end{document}

I have perhaps been over cautious checking the value of the x-separation after each box, but this may well be necessary in more complicated code.

Andrew Swann
  • 95,762