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 want to put some text in a box like \fcolorbox{red}{gray}{text} but such that moreover, the box is surrounded by an (non visible) space. So that if i put to such boxes near each other, there will be a small space between them. An such that if i put two such boxes in a \frac{}{} there will be some space between them and the fraction line. What would be the shortest way to do it ?

share|improve this question
4  
This is your seventh question on TeX.SX and we welcome new questions. However, you have not accepted any answer to any of your questions here and you have neither voted on any of the answers you have gotten. Please see the faq for details on accepting and voting. –  N.N. Dec 29 '11 at 14:12
    
You might want to rephrase your title and body to state your question more clearly. I had to read it twice to figure out if you now want to add some space or remove some unwanted one. E.g. "Add whitespace around a box" would be clearer than "Box surrounded by a space". –  Martin Scharrer Dec 29 '11 at 14:26
    
thanks for the comment :) –  nicolas roy Dec 29 '11 at 14:29
add comment

2 Answers

up vote 6 down vote accepted

The adjustbox package provides \marginbox{<value(s)>}{<content>} which adds an margin, i.e. whitespace around all sites of the content. You can give either one value for all sites, two for left/right and top/bottom or four for left, bottom, right and top. Values must be separated by spaces.

Examples:

\usepackage{adjustbox}
% ...

\marginbox{1pt}{\fcolorbox{blue}{red}{text}}% Adds 1sp space around the box

\marginbox{1pt 0pt}{\fcolorbox{blue}{red}{text}}% Adds 1sp space to the left and right of the box

\marginbox{1pt 0pt 2pt 0pt}{\fcolorbox{blue}{red}{text}}% Adds 1sp space to the left and 2pt to the right of the box

There is also the margin key for \adjustbox{<key=value,...>}{<content>} from the same package. It also provides bgcolor (background color) and cfbox (colored framed box) which can be used as an replacement for \fcolorbox (but you still need to load xcolor).

\documentclass{article}
\usepackage{xcolor}
\usepackage{adjustbox}

\newcommand{\mybox}{\adjustbox{cfbox=blue,bgcolor=red,margin=1pt}}

\begin{document}

\fcolorbox{red}{green}{text}\fcolorbox{blue}{red}{text}

\adjustbox{cfbox=red,bgcolor=green,margin=1pt}{text}\mybox{text}

\end{document}

Result

share|improve this answer
    
This will add 1pt on both sides "no matter what". –  egreg Dec 29 '11 at 14:48
    
@egreg: Well yes, it's a general example. For \frac you would need to add manually different margins dependent on the position. I personally don't think this will be used that often that you really need an automatic handling. –  Martin Scharrer Dec 29 '11 at 14:55
add comment
\DeclareRobustCommand{\roybox}[3]{%
  \ifdim\lastkern=1sp\,\fi\fcolorbox{#1}{#2}{#3}\kern1sp}

Now you can see the difference:

\fcolorbox{red}{green}{text}\fcolorbox{blue}{red}{text}

\roybox{red}{green}{text}\roybox{blue}{red}{text}

enter image description here

The small kern inserted by the macro will be unnoticeable (it's the minimum possible width and is less than the wavelength of visible light) if the box is not followed by another similar box: in this case, instead, it will be changed into a thin space \,.

I don't see any problem with fractions.

share|improve this answer
    
Nice idea to turn the 1sp into a \, but there's no guarantee a previous 1sp is caused by a previous \roybox. Perhaps turing the 1sp into a nastier constant would make it more robust? –  Marc van Dongen Jan 13 '12 at 8:35
    
@MarcvanDongen One is free to use any kern from 1 to 100sp (or maybe even more). However has a limited vision of what happened before it starts to examine a token for typesetting: kerns, skips, penalties or boxes. For the first three kinds the problem would be the same: there's no guarantee as to what left that item. One might use a box that allows to check for three dimensions: however \lastbox might be dangerous. –  egreg Jan 13 '12 at 10:02
add comment

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.