Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

I like using \boxed to emphasize my answers in homework problem sets. However, whenever I use superscripts/exponents inside the box, the box becomes taller than it seems it should. This causes two problems: the box looks bad, and it pushes other lines of text around.

Here's an example. Notice that the line spacing doesn't get changed until I use an exponent inside a box.

enter image description here

Is there an alternative to \boxed that won't have this problem? Or some other good way to solve this issue?

share|improve this question
2  
If you use \myboxed{...} instead of \boxed{...} where \newcommand{\myboxed}[1]{\smash{\boxed{#1}}}, then you can decide whether a removal of the boxing height works for you or not. \smash puts its contents in a box of height zero. –  Werner Nov 17 '11 at 21:34
    
Are you concerned with the horizontal or vertical spacing, or both? –  Werner Nov 17 '11 at 21:35
    
I had the exact same problem recently. My solution was to put my answers in a different color. If that's not available to you, I think Werner's subtle shaded background box is also a nice solution. Both highlight what you want to highlight in an unobtrusive, easily understood, and typographically non-ugly way. –  JohnJamesSmith Nov 18 '11 at 4:24
    
Related Question: mdframed: size frame to content. –  Peter Grill Dec 30 '12 at 18:44

6 Answers 6

You can use tikzmark as from this answer by Andrew Stacey to mark the endpoints where you want the box. The \MyBox macro as defined below accepts an optional first parameter which allows you to get fancy boxes.

enter image description here

Notes:

  • This does require two runs: the first to compute the positions of the box, and the second to draw it in the correct spot.

Known Issues:

  • This won't work if the text crosses line boundaries. If this is an issue and you are willing to consider highlighting instead of a box you should refer to Cool Text Highlighting in LaTeX.

  • This version will not adjust properly for text that adjusts the vertical spacing (ex. \dfrac{1}{2} from the amsmath package). However, an updated version of this that adjusts to the correct height is provided at A \boxed alternative with minimal spacing?

Code:

\documentclass[border=5pt]{standalone}
\usepackage{tikz}
\usetikzlibrary{calc,shapes}

\newcommand{\tikzmark}[1]{\tikz[overlay,remember picture] \node (#1) {};}
\newcommand{\DrawBox}[1][red]{%
    \tikz[overlay,remember picture]{
    \draw[#1]
      ($(bl)+(-0.2em,0.9em)$) rectangle
      ($(br)+(0.2em,-0.3em)$);}
}
\newcommand{\MyBox}[2][red]{\tikzmark{bl}#2\tikzmark{br}\DrawBox[#1]}

\begin{document}
    Lorem ipsum dolor sit amet. \MyBox{Lorem ipsum} dolor sit amet. Lorem ipsum dolor sit amet. 
    Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet.  Lorem 
    ipsum \MyBox[blue]{$3.29 \times 10^{29}$} dolor sit amet. Lorem ipsum dolor sit amet. Lorem 
    ipsum dolor sit amet. Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet. Lorem 
    \MyBox[draw=red,fill=yellow!20,,opacity=0.3]{$3.29 \times 10^{29}$} ipsum dolor sit amet. 
    Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet.
\end{document}
share|improve this answer
    
Is there a way to automate the size. more precisely, the solution does not work with fractions for example. –  projetmbc Nov 18 '11 at 12:54
    
@projetmbc: Yep, that is a known issue with this solution as I highlighted in the Known Issues section, but in this case the OP specifically was interested in using it in cases where the enclosed text did not effect the line inter line spacing. I to would be interested in that so perhaps post a follow up question if you can't find the solution here already, –  Peter Grill Nov 18 '11 at 16:27
    
I've posted my "hope" here : tex.stackexchange.com/questions/35319/… . –  projetmbc Nov 18 '11 at 18:15
    
@projetmbc: An updated tikz solution that adjusts for the vertical size of the text being enclosed has been posted at a boxed alternative with minimal spacing. –  Peter Grill Nov 19 '11 at 0:34

If you're interested in duplicating the behaviour of \boxed when it comes to the appearance, but remove the height-associated addition (supplied by \fboxrule and \fboxsep), you could define your own boxing macro based on \boxed. This is what \boxed is defined as in amsmath:

\newcommand{\boxed}[1]{\fbox{\m@th$\displaystyle#1$}}

As such, defining \myboxed to be

\newcommand{\myboxed}[1]{%
  \rlap{\hspace*{\dimexpr\fboxrule+\fboxsep\relax}%
    \phantom{\m@th$\displaystyle#1$}}%
    \smash{\boxed{#1}}}

would achieve this. Here is an MWE showing the resulting output for two paragraphs; the first uses \boxed, while the second uses \myboxed:

enter image description here

\documentclass{article}
\usepackage{amsmath}% http://ctan.org/pkg/amsmath
\makeatletter
\newcommand{\myboxed}[1]{%
  \rlap{\hspace*{\dimexpr\fboxrule+\fboxsep\relax}%
    \phantom{\m@th$\displaystyle#1$}}%
    \smash{\boxed{#1}}}
\makeatother
\begin{document}
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Lorem ipsum dolor sit amet, 
consectetur adipiscing elit. Aliquam ligula dui, blandit et posuere eu, varius in erat. 
Nunc ornare tellus in dui \boxed{3.299\times 10^{29}} tempus facilisis. Nunc ornare tellus 
in dui tempus facilisis. \boxed{3.299} Nulla vel dolor augue. Nulla commodo nulla et tortor 
aliquam nec lacinia odio sagittis. Aenean vitae interdum dui. Lorem ipsum dolor sit amet, 
consectetur adipiscing elit. $3.299\times 10^{29}$ Aliquam ligula dui, blandit et posuere 
eu, varius in erat. Nunc ornare tellus in dui tempus facilisis.


Lorem ipsum dolor sit amet, consectetur adipiscing elit. Lorem ipsum dolor sit amet, 
consectetur adipiscing elit. Aliquam ligula dui, blandit et posuere eu, varius in erat. 
Nunc ornare tellus in dui \myboxed{3.299\times 10^{29}} tempus facilisis. Nunc ornare 
tellus in dui tempus facilisis. \myboxed{3.299} Nulla vel dolor augue. Nulla commodo nulla 
et tortor aliquam nec lacinia odio sagittis. Aenean vitae interdum dui. Lorem ipsum dolor 
sit amet, consectetur adipiscing elit. $3.299\times 10^{29}$ Aliquam ligula dui, blandit et 
posuere eu, varius in erat. Nunc ornare tellus in dui tempus facilisis.

\end{document}

You'll note that it also highlights some of the problems with removing the vertical boxing adjustment. @Boris' suggestion to reduce \fboxsep would help. You could also print something more subtle like a shaded box and remove the solid rule. Here's an alternative to \myboxed that achieves that:

enter image description here

\documentclass{article}
\usepackage{amsmath}% http://ctan.org/pkg/amsmath
\usepackage{xcolor}% http://ctan.org/pkg/xcolor
\makeatletter
\newcommand{\myboxed}[2][black!5]{%
  \setlength{\fboxrule}{0pt} \setlength{\fboxsep}{1pt}%
  \rlap{\hspace*{\fboxsep}%
    \phantom{\m@th$\displaystyle#2$}}%
    \smash{\colorbox{#1}{\m@th$\displaystyle#2$}}}
\makeatother
\begin{document}
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Lorem ipsum dolor sit amet, 
consectetur adipiscing elit. Aliquam ligula dui, blandit et posuere eu, varius in erat. 
Nunc ornare tellus in dui \boxed{3.299\times 10^{29}} tempus facilisis. Nunc ornare tellus 
in dui tempus facilisis. \boxed{3.299} Nulla vel dolor augue. Nulla commodo nulla et tortor 
aliquam nec lacinia odio sagittis. Aenean vitae interdum dui. Lorem ipsum dolor sit amet, 
consectetur adipiscing elit. $3.299\times 10^{29}$ Aliquam ligula dui, blandit et posuere 
eu, varius in erat. Nunc ornare tellus in dui tempus facilisis.


Lorem ipsum dolor sit amet, consectetur adipiscing elit. Lorem ipsum dolor sit amet, 
consectetur adipiscing elit. Aliquam ligula dui, blandit et posuere eu, varius in erat. 
Nunc ornare tellus in dui \myboxed{3.299\times 10^{29}} tempus facilisis. Nunc ornare 
tellus in dui tempus facilisis. \myboxed{3.299} Nulla vel dolor augue. Nulla commodo nulla 
et tortor aliquam nec lacinia odio sagittis. Aenean vitae interdum dui. Lorem ipsum dolor 
sit amet, consectetur adipiscing elit. $3.299\times 10^{29}$ Aliquam ligula dui, blandit et 
posuere eu, varius in erat. Nunc ornare tellus in dui tempus facilisis.

\end{document}

\myboxed has been fitted with an optional argument to modify the colour (default is black!5 or 5% black):

\myboxed[<colour>]{<stuff>}
share|improve this answer

Change the two values for the depth and the height to whatever you need:

\documentclass[a5paper]{article}
\usepackage{mathtools}
\newsavebox\FBox
\def\Boxed#1{\sbox\FBox{%\fboxsep=1.5pt% may also be useful
   $\boxed{#1}$}\rule[-1.2\dp\FBox]{0pt}{1.2\ht\FBox}\usebox\FBox}
\begin{document}

This is some long text This is some long text 
This is some long text This is some long text 
\Boxed{3.299\times10^{29}} This is some long text 
This is some long text This is some long text This is some long text 
This is some long text This is some \Boxed{3.299} long text 

\end{document}

enter image description here

share|improve this answer

Another option could be using a tcbox from tcolorbox package. Recent (v 3.05) tcolorbox versions offer size option which easily fixes some default box characteristics (size, corners, border width, ...). Its size=tight definition could be too tight but with help of boxsep=1pt the result is good to my taste.

enter image description here

\documentclass{article}
\usepackage[most]{tcolorbox}

\newtcbox{\MyBox}[1][red]{on line, size=tight, boxsep=1pt, colframe=#1!50!black, colback=#1!10!white}

\begin{document}
    Lorem ipsum dolor sit amet. \MyBox{Lorem ipsum} dolor sit amet. Lorem ipsum dolor sit amet. 
    Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet.  Lorem 
    ipsum \MyBox[blue]{$3.29 \times 10^{29}$} dolor sit amet. Lorem ipsum dolor sit amet. Lorem 
    ipsum dolor sit amet. Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet. Lorem 
    \MyBox[green]{$3.29 \times 10^{29}$} ipsum dolor sit amet. 
    Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet.
\end{document}
share|improve this answer

Solution 1

Decrease the separation between the box and the text:

\setlength\fboxsep{1pt}
\lipsum*[1]$\boxed{10^x}$\lipsum[2]

Solution 2

Decrease the size of the boxed formula:

\lipsum*[1]$\footnotesize\boxed{10^x}$\lipsum[2]
share|improve this answer

Here is one solution that I came up with today that solves the original question, but also satisfies the additional requirement that all boxes are the same height.

\usepackage[customcolors,nofill,norndcorners]{hf-tikz}
\hfsetbordercolor{black}

\newcounter{boxCounter}
\newcommand{\mybox}[1]{
    \stepcounter{boxCounter}
    \hspace{0.3em}
    \tikzmarkin[thin,left offset=-0.15em,right offset=0.15em,below offset=-0.35em,above offset=0.9em]{\theboxCounter}#1\tikzmarkend{\theboxCounter}
    \hspace{0.3em}
}

For example, $\boxed{c}$ and $\boxed{f(c)}$ on the same line would result in boxes of different heights and additionally affects the interline spacing.

Using my solution, the boxes (i.e. $\mybox{c}$ and $\mybox{f(c)}$) are much tighter around the content, are the same height, and the interline spacing is unaffected (i.e. it remains as if no boxes were drawn).

Two caveats:

  • You may have to adjust the above and below offset in the \mybox command to suit your font, etc.
  • This solution will not adjust properly for text that adjusts the vertical spacing (e.g. \dfrac{1}{2} from the amsmath package), however, this is a side affect of the requirement that all boxes be the same height on a page.

Thus while it avoids the need for vphantom's in multiple boxes, it may not suit your needs. However, it could be adapted to handle content which does adjust the vertical spacing.

share|improve this answer
    
Please always post a complete compilable example including \documentclass. If you want one that adjusts to various heights, see A \boxed alternative with minimal spacing?. –  Peter Grill Apr 3 at 20:11

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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