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?

7
  • @CarLaTeX that just gives me an error "Choice 'base' unknown in choice key '/tikz/align'" Commented Feb 20, 2018 at 11:15
  • Hmm, this works, but isn't ideal because I'm manually tweaking the values: Some text \tikz[baseline] \node[yshift=1mm,rectangle,rounded corners=0.5mm,text=white,fill=black!65,inner sep=2pt] {\scriptsize{}foo}; more text. Commented Feb 20, 2018 at 11:26
  • @CarLaTeX: Did you mean anchor=base?
    – user31729
    Commented Feb 20, 2018 at 11:30
  • @CarLaTeX @Christian Hupfer anchor=base also does not work, at least not on its own. Commented Feb 20, 2018 at 11:35
  • @KentBoogaart: See my answer below. It works for me, even in ShareLaTeX
    – user31729
    Commented Feb 20, 2018 at 11:45

1 Answer 1

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

0

You must log in to answer this question.

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