How to draw a partial or incomplete box around one or more words within a paragraph

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


Is there an easy, general way to do this, perhaps with Tikz?

I cannot understand the tendency of people to use a sledgehammer to crack a nut. Perhaps with tabular?

enter image description here

\documentclass{article}
\begin{document}
\tabcolsep.2em
Un voyage \begin{tabular}{|c }\hline dans\\\hline \end{tabular} l'space. 
Un voyage \begin{tabular}{|c|}\hline dans\\       \end{tabular} l'space. 
Un voyage \begin{tabular}{ c|}\hline dans\\\hline \end{tabular} l'space. 
Un voyage \begin{tabular}{|c|}       dans\\\hline \end{tabular} l'space. 
\end{document}

If you hate tabular, some \makebox, \rule and \vrule commands can do the same work. However, it is tedious finding manually the right widths of the horizontal rules. But this is solved using \widthof ofthe calc package. This make a complete framed box with automatic width:

\vrule%
\makebox[0pt][l]{\rule[-.25em]{\widthof{dans}+.2em}{.4pt}}% 
\makebox[0pt][l]{\rule[.85em]{\widthof{dans}+.2em}{.4pt}}%
\makebox[\widthof{dans}+.2em][c]{dans}%
\vrule\ 

Simply removing rows 1,2,3 or 5 in the above code you can obtain the incomplete boxes.

Convert both solutions in macros is straightforward. For simplicity, instead of a single macro with two arguments, I suggest make four macros where the text is the unique argument. For instance:

\documentclass{article}
\usepackage{calc}
\newcommand\openleftbox[1]{%
\vrule
\makebox[0pt][l]{\rule[-.25em]{\widthof{#1}+.2em}{.4pt}}% 
\makebox[0pt][l]{\rule[.85em]{\widthof{#1}+.2em}{.4pt}}%
\makebox[\widthof{#1}+.2em][c]{#1}%
%\vrule\ 
}
\begin{document}
Un voyage \openleftbox{dans} l'space. 
\end{document}

Another solution with tcbox (from tcolorbox.

\documentclass{article}
\usepackage{tcolorbox}
\usepackage{lmodern}

\newtcbox{\lbox}[1][]{on line, sharp corners, colback=white, colframe=black, size=small, leftrule=0pt,#1}

\newtcbox{\rbox}[1][]{on line, sharp corners, colback=white, colframe=black, size=small, rightrule=0pt,#1}

\newtcbox{\tbox}[1][]{on line, sharp corners, colback=white, colframe=black, size=small, toprule=0pt,#1}

\newtcbox{\bbox}[1][]{on line, sharp corners, colback=white, colframe=black, size=small, bottomrule=0pt,#1}

\begin{document}

The \lbox{quick} \tbox{brown} \bbox{fox} \rbox{jumps} over the \lbox[colback=red!30, colframe=blue]{lazy dog}.

\end{document}

enter image description here