40

If I inline a bounded line segment like this:

the distance \tikz{ \draw [|-|] (0,0) -- (5ex,0); } overarching the activities

how can I make the line appear vertically centered on the line (automatically taking into account the line height).

Emit Taste
  • 4,404

2 Answers2

46

You can directly tell where the baseline should meet the picture using the baseline option

\documentclass{article}
\usepackage{tikz}
\begin{document}
the distance \tikz[baseline=-0.5ex]{ \draw [|-|] (0,0) -- (5ex,0); } overarching the activities
\end{document}

enter image description here

percusse
  • 157,807
3

Neither percusse's nor Claudio's answer worked for me when I wanted to insert a small inline graph. Instead Martin's suggestion of \raisebox did work.

Example

Example

Code

\documentclass{article}
\usepackage{pgfplots} %for drawing of graphs
\pgfplotsset{compat=newest}
\setlength{\parindent}{0pt} 

\begin{document}
The original one 
\frame{
\begin{tikzpicture}
    \begin{axis}[width=10 ex, height = 3 ex, 
                 scale only axis, hide axis]
        \addplot[black] {rand};
    \end{axis};
\end{tikzpicture}}
is too high.

And percusse's \verb|[baseline=-0.5 ex]|
\frame{
\begin{tikzpicture}[baseline=-0.5ex]
    \begin{axis}[width=10 ex, height = 3 ex, 
                 scale only axis, hide axis]
        \addplot[black] {rand};
    \end{axis};
\end{tikzpicture}}
does not work, it only shifts 
\frame{
\begin{tikzpicture}
    \begin{axis}[width=10 ex, height = 3 ex, 
                 scale only axis, hide axis]
        \addplot[black] {rand};
    \end{axis};
\end{tikzpicture}}
the graph upwards a little.

\verb|\raisebox{-.5 ex}|
\raisebox{-.5 ex}{\frame{
\begin{tikzpicture}
    \begin{axis}[
    width=10 ex, height = 3 ex, scale only axis,
    hide axis,
    ]
    \addplot[black] {rand};
    \end{axis};
\end{tikzpicture}}}
does work in this case, but I don't know why.
\end{document}
Roald
  • 1,245
  • 1
    Because you are trying to push down -0.5ex adjust the baseline position for that example. As I commented, you can adjust with baseline to be wherever you want. – percusse Sep 08 '16 at 16:32
  • 1
    @percusse Sorry I'm not sure what you mean. You mean you can also push them in a positive direction? I've tried that, but that does nothing. – Roald Sep 09 '16 at 16:23
  • Question: is 3 ex the height that won't mess with the interline? Also, to answer your question, you wanted to push the baseline 1 ex down (positive value, not negative), not just 0.5 ex (you got the amount and sign wrong in your reply) since the total height is 3 ex and you want it centred at mid x's height, which is 0.5 ex. – Atcold Sep 29 '22 at 16:58