How to add a small rounded rectangle around a word inline

Yes, it is possible with TikZ. You can do this in two ways:

  • by including the code directly in the text.
  • by creating a LaTeX command named autour which contains the TikZ code.

Here is an example of two possibilities that have different parameters in order to visually show their effects.

autour

\documentclass{article}
\usepackage{tikz}
\newcommand{\autour}[1]{\tikz[baseline=(X.base)]\node [draw=red,fill=gray!40,semithick,rectangle,inner sep=2pt, rounded corners=3pt] (X) {#1};}

\begin{document}

Quick brown fox \tikz[baseline=(X.base)]\node [draw=black,fill=cyan!20,thick,rectangle,inner sep=3pt, rounded corners=4pt] (X) {jumped}; over the lazy dog.
\bigskip

Quick brown fox \autour{jumped} over the lazy dog.
\end{document}

Alternative with tcolorbox

A simple adaptation of the code on page 16 of the manual indicated by @CarlaTeX in its commentary.

entoure

\documentclass{article}
\usepackage{tcolorbox}

\newtcbox{\entoure}[1][red]{on line,
arc=3pt,colback=#1!10!white,colframe=#1!50!black,
before upper={\rule[-3pt]{0pt}{10pt}},boxrule=1pt,
boxsep=0pt,left=2pt,right=2pt,top=1pt,bottom=.5pt}

\begin{document}

Quick brown fox \entoure{jumped} over the lazy dog.

Quick brown fox \entoure[blue]{jumped} over the lazy dog.
\end{document}

This is a bit similar to AndréC's answer but to point out that there so some extent \tikzmarknode, which comes with the really cool tikzmark library, got reinvented. And tikzmark allows you to do much more, of course.

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{tikzmark}

\begin{document}

\section*{Some basic examples}

The quick brown fox \tikzmarknode[draw,inner sep=2pt,rounded corners,fill=cyan!30]{A}{jumped} over the lazy dog.

\bigskip

The quick brown fox \tikzmarknode[draw,inner sep=2pt,rounded corners,fill=red!30]{B}{jumped} over the lazy dog.

\section*{Some more elaborate examples}

As it is well known,
\[ \sum\limits_{\tikzmarknode[rounded corners,fill=blue!30,inner sep=1pt]{k1}{k}=1}^\infty
k~=~-\frac{1}{12}\;,\]
where $\tikzmarknode[rounded corners,fill=blue!30,inner sep=2pt]{k2}{k}$ is a summation index.

\tikz[overlay,remember picture]{\draw[latex-latex] (B) to[bend left] (A);
\draw[latex-latex] (k1) to[bend right] (k2);
}
\end{document}

enter image description here

Tags:

Boxes

Tikz Pgf