12

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.

10

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

  • I wonder how does this look like in a paragraph, surrounded by running text. – Matsmath Aug 9 '16 at 11:44
  • 1
    @Matsmath It will behave like \fbox, subject to changes in \fboxrule and \fboxsep. – Steven B. Segletes Aug 9 '16 at 11:46
  • Beautiful solution. I wonder, is there any easy way to modify this code to remove two of the sides--say, the top and left, at one time? – twoblackboxes Aug 9 '16 at 12:48
  • @twoblackboxes Please see revision of 1st answer to see how it can be done. – Steven B. Segletes Aug 9 '16 at 13:26
  • @StevenB.Segletes This is really excellent work. Thank you so much! – twoblackboxes Aug 9 '16 at 13:33
4

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}
  • Sledgehammer? If you go to my profile, you will see that "I enjoy... reinventing the wheel" Perhaps I should add that I enjoy cracking nuts with a sledgehammer. – Steven B. Segletes Aug 10 '16 at 3:22
  • 1
    @StevenB.Segletes In this way? – Fran Aug 10 '16 at 3:38
2

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

2

This can be achieved with the efbox package.

Examples from the documentation:

\documentclass[convert]{standalone}

\usepackage{efbox}

\begin{document}

\efbox{Foo}
\efbox[rightline=false,topline=false]{Foo}
\efbox[topline,backgroundcolor=red]{Foo}
\efbox[linewidth=2pt,font=\Large]{Large Foo}
\efbox[rightline=false,topline=false,linecolor=blue,linewidth=2pt]{Foo}
\efbox[margin=10pt,backgroundcolor=yellow,font=\ttfamily\itshape]{Italic Typewriter Foo}
\efbox[linewidth=3pt,margin=5pt,backgroundcolor=red]{Foo}
\efbox[hidealllines,backgroundcolor=red]{Foo}
\efbox{Foo}
\efbox[hidealllines,backgroundcolor=red,margin=15pt]{Foo}
\efbox[margin=15pt,linewidth=5pt]{Foo}
\efbox[bottomline=false,rightline=false,linewidth=2pt,margin=1pt,backgroundcolor=yellow]{Foo}
\efbox{Foo}

\end{document}

enter image description here

  • This is the easiest method presented here. However, I don't know why, but it is bugged. I managed to draw what i want after many experiments, adding backslashes in front etc.. – Nikos Feb 22 '17 at 10:27
0

Package fbox has been recently added to CTAN. This package adds an optional parameter to \fbox. This new parameter allows to declare which box sides should be drawn.

\documentclass{article}
\usepackage{fbox}

\begin{document}

The \fbox[tb]{quick} \fbox[lr]{brown} \fbox[ltb]{fox} \fbox[trb]{jumps} \fbox[lb]{over} \fbox[tr]{the} \fbox{lazy dog}.

\end{document}

enter image description here

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.