4

I have a bit of TikZ in my text, namely:

\documentclass[]{standalone}
  \usepackage{tikz}
  \begin{document}
    Some text (\tikz{\node[scale=0.5,solid,draw,thick,blue,rectangle,fill=white] {A};}), and some more
  \end{document}

This produces the blue rectangle as expected, but it is not aligned with the bottom of the parentheses, which I want.

Is there a simple way to shift an object or even text within a line vertically, while keeping the rest? Or is there some workaround in TikZ that might be able to do so?

Markus
  • 1,365
  • 1
    See http://tex.stackexchange.com/q/65932/15925 . You use the baseline option to do this. – Andrew Swann Apr 25 '13 at 15:59
  • If I use Some text (\tikz[baseline=(O.base)]{\node (O) [scale=0.5,solid,draw,thick,blue,rectangle,fill=white,minimum height=0.5cm,minimum width=0.5cm] {a};}), (\tikz[baseline=(O.base)]{\node (O) [scale=0.5,solid,draw,thick,blue,rectangle,fill=white] {d};}) and some more The two blue boxes are not aligned though... – Markus Apr 25 '13 at 16:09

1 Answers1

5

This is a bit like the Chain Tutorial in the pgfmanual. It is best to specify text height and text depth for the nodes in this case. For example as follows:

Sample output

\documentclass{article}

\usepackage{tikz}

\begin{document}
\tikzset{every node/.style={anchor=base,scale=0.5,draw,thick,blue,
text height=1.7ex,text depth=0.3ex, minimum width=1em}}

Some text 
(\tikz[baseline]{\node {a};}), 
(\tikz[baseline]{\node {d};}) 

More demonstration
(\tikz[baseline]{\node {a};}\tikz[baseline]{\node {d};}%
\hbox to 0pt{\hss\tikz{\draw[very thin,red] (0,0) -- (4cm,0);}})  

\end{document}

I have specified anchor=base for the nodes, so the baseline of the text in these nodes lies on the x-axis. Then it is sufficient to just pass the plain baseline option to \tikz for the vertical alignment.

In general, you can set baseline to some vertical dimension to shift material

Sample output

\documentclass{article}

\usepackage{tikz}

\begin{document}

\tikzset{every node/.style={anchor=base,draw,thick,blue,
text height=1.7ex,text depth=0.3ex, minimum width=1em,scale=0.5}}

Some text 
\tikz[baseline]{\node {a};}
\tikz[baseline=0.1ex]{\node {a};}
\tikz[baseline=0.3ex]{\node {a};}
\tikz[baseline=0.7ex]{\node {a};}
\tikz[baseline=1ex]{\node {a};}
\tikz[baseline=2ex]{\node {a};}
\tikz[baseline=4ex]{\node {a};}
\tikz[baseline=-0.3ex]{\node {a};}
\tikz[baseline=-0.5ex]{\node {a};}
\tikz[baseline=-1ex]{\node {a};}
\tikz[baseline=-2.5ex]{\node {a};}
\tikz[baseline=-4ex]{\node {a};}
for a dance.

\end{document}

or use a node as in the linked question.

I have specified dimensions in terms of ex and em units which reflect the size of the font used, rather than absolute units such as cm.

Andrew Swann
  • 95,762