6

I would like to put a tikzpicture in the middle of a text line, but I can't find how to specify vertical positionning. Explications :

\documentclass{article}
\usepackage[english]{babel}
\usepackage{tikz}

\begin{document}
Lorem ipsum dolor sit amet, consectetur adipiscing elit
\begin{tikzpicture}
 \draw (2.25, 2) -- (2,0);
 \draw (.7, 0) -- (4,0) -- (2.25, 2) node[right] {$A_i$} -- (.7,0);
 \draw (3,0) -- (2.125, 1) node[right] {$A_j$} -- (1.4,0);
 \draw (1.05, 0) node[below] {$v$} (2,0) node[below] {$w$} (3.5, 0) node[below] {$x$} (4.5, 0);
\end{tikzpicture}

\end{document}

gives me something like

this alignment

but I would like to get something like

this other alignment

I know vertical positioning options exist for tabular and array (\begin{array}[b]{rcl} for a bottom alignment for exemple), but I can't find something similar for tikzpictures.

Thanks for your help.

4 Answers 4

4

Here's another approach that works if you want to put more text on the same line. It puts the figure in a box and then lowers the box by half its height.

\documentclass{article}
\usepackage[english]{babel}
\usepackage{tikz}

\newbox\mybox
\def\centerfigure#1{%
    \setbox\mybox\hbox{#1}%
    \raisebox{-0.5\dimexpr\ht\mybox+\dp\mybox}{\copy\mybox}%
}
\begin{document}
Lorem ipsum dolor sit amet,
\centerfigure{\begin{tikzpicture}
 \draw (2.25, 2) -- (2,0);
 \draw (.7, 0) -- (4,0) -- (2.25, 2) node[right] {$A_i$} -- (.7,0);
 \draw (3,0) -- (2.125, 1) node[right] {$A_j$} -- (1.4,0);
 \draw (1.05, 0) node[below] {$v$} (2,0) node[below] {$w$} (3.5, 0) node[below] {$x$} (4.5, 0);
\end{tikzpicture}} more text
\end{document} 
6

Coincidentally, I just solved a similar problem. Applied to your example, that works out to be this:

\documentclass{article}
\usepackage{tikz}

\begin{document}
Lorem ipsum dolor sit amet,%
\begin{tikzpicture}[baseline=(AA.base)]%
 \draw (2.25, 2) -- (2,0);
 \draw (.7, 0) -- (4,0) -- (2.25, 2) node[right] {$A_i$} -- (.7,0);
 \draw (3,0) -- (2.125, 1) node[right](AA) {$A_j$} -- (1.4,0);
 \draw (1.05, 0) node[below] {$v$} (2,0) node[below] {$w$} (3.5, 0) node[below] {$x$};
\end{tikzpicture}%
more text
\end{document} 

Centering tikz figure on baseline.

True, that this works happens to be due to the nature of the figure. Admittedly, Hood Chatham's solution is more general.

Changing [baseline=(AA.base)] to [baseline=(AA.south)] produces this:

Alternate version

2

I think using math mode and \vcenter is the way to go for vertically centering stuff on the current text line:

\documentclass{article}
\usepackage[english]{babel}
\usepackage{tikz}

\begin{document}
Lorem ipsum dolor sit amet, consectetur adipiscing elit
$\vcenter{\begin{tikzpicture}
 \draw (2.25, 2) -- (2,0);
 \draw (.7, 0) -- (4,0) -- (2.25, 2) node[right] {$A_i$} -- (.7,0);
 \draw (3,0) -- (2.125, 1) node[right] {$A_j$} -- (1.4,0);
 \draw (1.05, 0) node[below] {$v$} (2,0) node[below] {$w$} (3.5, 0) node[below] {$x$} (4.5, 0);
\end{tikzpicture}}$

\end{document}
3
  • Thanks for that answer, but the \vcenter environment ends the line, so I can't continue the line with some text.
    – Nathaniel
    Commented Oct 21, 2017 at 14:22
  • That's annoying. I'll see if I can fix that... Commented Oct 21, 2017 at 14:25
  • See my second answer. Commented Oct 21, 2017 at 14:32
1

I've modified sgmoye solution to be more general. Seems to work perfectly but I haven't tested it thoroughly.

\documentclass{article}
\usepackage{tikz}

\begin{document}
Lorem ipsum dolor sit amet, consectetur adipiscing elit
\begin{tikzpicture}[baseline=(current bounding box)]
 \draw (2.25, 2) -- (2,0);
 \draw (.7, 0) -- (4,0) -- (2.25, 2) node[right] {$A_i$} -- (.7,0);
 \draw (3,0) -- (2.125, 1) node[right] {$A_j$} -- (1.4,0);
 \draw (1.05, 0) node[below] {$v$} (2,0) node[below] {$w$} (3.5, 0) node[below] {$x$} (4.5, 0);
\end{tikzpicture}

\end{document}

rendered latex document

Note that you can also use baseline=(current bounding box.north) to position the figure right below the text.

I've found this solution in pgf manual.

1
  • This is IMHO the best solution.
    – Clément
    Commented Sep 12, 2022 at 18:29

You must log in to answer this question.

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