[Tex/LaTex] How to draw a partial or incomplete box around one or more words within a paragraph

boxesfancyboxtikz-pgf

I am trying to draw a partial box around one or more words within a paragraph–that is, a rectangle with one or more sides missing. I have read the documentation for the fancybox package and it doesn't seem to be able to do what I want. I was able to produce thus ugly hack:

enter image description here

That's just a vertical bar | followed by a word with \overline and \uline. The trick is to connect the vertical and horizontal lines so that you have a rectangle missing just one side.

Is there an easy, general way to do this, perhaps with Tikz? I don't know enough about that package to be able to do this myself, but I'm wondering if there might be other solutions too.

Best Answer

This solution EDITED to allow multiple sides to be stricken.

Made into a macro \partbox{<sides>}{<content>} where sides are the sides to be stricken, in any combination (in any order) of l, b, r, or t. By using \fbox, it is customizeable with the use of \fboxsep and \fboxrule.

Being a box, the words if more than one, will not be permitted to break across a line.

\documentclass{article}
\usepackage{trimclip}
\newif\iflclip
\newif\ifbclip
\newif\ifrclip
\newif\iftclip
\def\CLIP{\dimexpr\fboxrule+.2pt\relax}
\def\nulclip{0pt}
\newcommand\partbox[2]{%
  \lclipfalse\bclipfalse\rclipfalse\tclipfalse%
  \let\lkern\relax\let\rkern\relax%
  \let\lclip\nulclip\let\bclip\nulclip\let\rclip\nulclip\let\tclip\nulclip%
  \parseclip#1\relax\relax%
  \iflclip\def\lkern{\kern\CLIP}\def\lclip{\CLIP}\fi
  \ifbclip\def\bclip{\CLIP}\fi
  \ifrclip\def\rkern{\kern\CLIP}\def\rclip{\CLIP}\fi
  \iftclip\def\tclip{\CLIP}\fi
  \lkern\clipbox{\lclip{} \bclip{} \rclip{} \tclip}{\fbox{#2}}\rkern%
}
\def\parseclip#1#2\relax{%
  \ifx l#1\lcliptrue\else
  \ifx b#1\bcliptrue\else
  \ifx r#1\rcliptrue\else
  \ifx t#1\tcliptrue\else
  \fi\fi\fi\fi
  \ifx\relax#2\relax\else\parseclip#2\relax\fi
}
\parskip 1ex
\begin{document}
\partbox{l}{dans} \partbox{b}{dans} \partbox{r}{dans} \partbox{t}{dans}

\partbox{lt}{dans} \partbox{lr}{dans} \partbox{lb}{dans}

\partbox{tb}{dans} \partbox{tr}{dans}

\partbox{br}{dans}

\partbox{rlt}{dans} \partbox{rbt}{dans} \partbox{blt}{dans} \partbox{blr}{dans}
\end{document}

enter image description here

Just to show the ability to use \fboxsep and \fboxrule, here is the identical result, but with \fboxsep=0pt\relax\fboxrule=1pt\relax set at the beginning of the document:

enter image description here

If one wishes it not to interfere with linespacing, then this tweak should work, adding a \vphantom and \smash. Of course, it will not prevent overlap, if \fboxsep and/or \fboxrule are set large enough (NOTE: this solution is still the original variety, only allowing a single side to be stricken):

\documentclass{article}
\usepackage[nopar]{lipsum}
\usepackage{trimclip}
\def\CLIP{\dimexpr\fboxrule+.2pt\relax}
\newcommand\partbox[2]{\leavevmode\vphantom{#2}\smash{%
  \ifx#1l\clipbox{\CLIP{} 0pt 0pt 0pt}{\fbox{#2}}\else
  \ifx#1b\clipbox{0pt \CLIP{} 0pt 0pt}{\fbox{#2}}\else
  \ifx#1r\clipbox{0pt 0pt \CLIP{} 0pt}{\fbox{#2}}\else
  \ifx#1t\clipbox{0pt 0pt 0pt \CLIP{}}{\fbox{#2}}\else
  \fi\fi\fi\fi
}}
\parskip 1ex
\begin{document}
\lipsum[4] \partbox{l}{dans}
\lipsum[4] \partbox{b}{dans}
\lipsum[4] \partbox{r}{dans}
\lipsum[4] \partbox{t}{dans}
\lipsum[4] 
\end{document}

enter image description here

Related Question