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


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.

  \lkern\clipbox{\lclip{} \bclip{} \rclip{} \tclip}{\fbox{#2}}\rkern%
  \ifx l#1\lcliptrue\else
  \ifx b#1\bcliptrue\else
  \ifx r#1\rcliptrue\else
  \ifx t#1\tcliptrue\else
\parskip 1ex
\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{rlt}{dans} \partbox{rbt}{dans} \partbox{blt}{dans} \partbox{blr}{dans}

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):

  \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
\parskip 1ex
\lipsum[4] \partbox{l}{dans}
\lipsum[4] \partbox{b}{dans}
\lipsum[4] \partbox{r}{dans}
\lipsum[4] \partbox{t}{dans}

enter image description here

Related Question