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 ?
- 8,167
-
5This 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#howtoask] 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
2 Answers
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}

- 262,582
-
1
-
@egreg: Well yes, it's a general example. For
\fracyou 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
\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}

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.
- 1,121,712
-
Nice idea to turn the
1spinto a\,but there's no guarantee a previous1spis caused by a previous\roybox. Perhaps turing the1spinto a nastier constant would make it more robust? – Jan 13 '12 at 08: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
\lastboxmight be dangerous. – egreg Jan 13 '12 at 10:02