3

Is it possible to write a command using TikZ to produce a small rounded and colored box around a piece of text, inside sentence? I know the \framedbox command gives a rectangle around a piece of text (but not a rounded rectangle as far as I know).

I guess one could also adjust the color of the background when using framebox (using tips from answers to this question), but I don't think it could help with other factors such as the box's edge-width, the amount of roundness, etc.

The following picture was produced with Inkscape for the purpose of the question.

enter image description here

2 Answers2

4

Either use TikZ directly or tcolorbox with \tcbox and a lot configurable settings.

If such boxes should appear more than once, a \newtcbox is appropiate, setting the options there and just saying \inlinebox{foo} etc.

As can be seen from the direct usage of \tcbox that the margins are too wide, but the \inlinebox has a nicer look, there the margins are set with left=... and right=... and boxsep=...

enter image description here

\documentclass{article}

\PassOptionsToPackage{svgnames,x11names,dvipsnames}{xcolor}
\usepackage[most]{tcolorbox}

\newtcbox{\inlinebox}[1][]{enhanced,
 box align=base,
 nobeforeafter,
 colback=cyan,
 colframe=Green4,
 size=small,
 left=0pt,
 right=0pt,
 boxsep=2pt,
 #1}

\begin{document}

Hello \tcbox[enhanced,box align=base,nobeforeafter,colback=cyan,colframe=Green4,size=small]{World} -- what do you expect from the year \inlinebox{2019}?


\end{document}
2

Another option is tikzmark, which less options than tcolorbox, but allows you to connect different boxes and also detect the mode you are in (text vs. math etc.).

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{tikzmark}
\begin{document}
Here is some text and \tikzmarknode[fill=cyan,fill
opacity=0.3,draw=green!60!black,thick,rounded corners,inner sep=2pt,text
opacity=1]{test}{something about relativity}.
\[\tikzmarknode[fill=cyan,fill
opacity=0.3,draw=green!60!black,thick,rounded corners,inner sep=2pt,text
opacity=1]{testeq}{E~=~m\,c^2}
\begin{tikzpicture}[overlay,remember picture]
\draw[thick,cyan,latex-latex] (test.-5) to[out=-90,in=0] (testeq.east);
\end{tikzpicture}\]
\end{document}

enter image description here