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.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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
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
up vote 8 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.


% ...

\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).








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

Now you can see the difference:



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

Your Answer


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.