12

I'm trying to incorporate tikz nodes within running text, but have them align nicely with the text surrounding them. My MWE is:

% !TEX program = xelatex

\documentclass{scrbook}

\usepackage{tikz}

\begin{document}

Some text \tikz \node[rectangle,rounded corners=0.5mm,text=white,fill=black!65,inner sep=2pt] {\scriptsize{}foo}; more text.

\end{document}

This produces:

enter image description here

I would like to shift the node down so that the baseline of "foo" aligns with the baseline of the surrounding text. There's quite a bit of information on aligning relative to other nodes, but really all I need to do is shift it down vertically by a set amount, but I can't figure out how.

What is the easiest/best way for me to shift the entire node down so that baselines align?

Kent Boogaart
  • 664
  • 4
  • 15

1 Answers1

12

Use \tikz[baseline] and anchor=base as an option to the node or use tcolorbox with box align=base

\documentclass{scrbook}

\usepackage[most]{tcolorbox}

\usepackage{tikz}

\begin{document}

Some text \tikz[baseline]{%
\node[rectangle,rounded corners=0.5mm,text=white,fill=black!65,inner sep=2pt,anchor=base] (A) {\scriptsize{}foo};} more text.

Some text \tcbox[enhanced,frame hidden,box align=base,nobeforeafter,left=0pt,right=0pt,top=0pt,bottom=0pt,arc=0.5mm,fontupper=\scriptsize,colupper=white,,colback=black!65]{foo} more text
\end{document}

enter image description here