8

I want to add a box with a very light pink background and red border around a word but inline and within a sentence.

i.e. something like this (drawn with inkscape)

enter image description here

I know the mdframed package can be used for placing boxes around paragraphs, but it does not seem to work inline and also give colored boxes.

Possibly TikZ is a solution? From the little I know of TikZ, it can be used to draw pictures inline inside a sentence.

  • See tcolorbox package documentation at the bottom of page 16. – CarLaTeX Nov 7 '18 at 20:46
9

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}
7

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

  • So tikzmark allows you to connect one or more marked positions on the page, is that correct? – smilingbuddha Nov 7 '18 at 22:24
  • @smilingbuddha You're welcome. Let me also mention that tikzmark does actually much more than what I showed. I add some more info. One particularly nice feature is that it detects whether or not you are in math mode, and if you are in math mode, in which style. So it always gives you the right fonts and sizes, things that, sorry to say that so openly, AndrèC's proposal fails to achieve. And given how complicated it is to achieve this, I am deeply impressed by what LoopSpace has done. – marmot Nov 7 '18 at 22:32
  • I have just discovered thanks to your answer the great possibilities of tikzmark that I still did not know. Unfortunately, having to write the complete tikzmark code every time you want to frame a word is not very practical to use here. You should add a LaTeX command that would simplify its use for our friend @smilingbuddha. – AndréC Nov 8 '18 at 5:48

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.