# How to draw a colored framebox without filling the background?

I like to draw a \fbox{..} which is colored. The xcolor package gives

\fcolorbox{<frame color>}{<background color>}{<text>}


But this forces me to set a background color. I like to have the background not filled at all, but simply the frame drawn in a color. Setting it to the paper color (white normally) would be a workaround but doesn't work for shaded backgrounds.

Is there another macro for this or a possibility to set <background color> to transparent or have it ignored?

PS: I like to do this without using anything as big as TikZ or PSTricks. This is actually for use in a package and shouldn't force users to also load these packages.

-
Use TikZ. ;-) –  Caramdir Jul 13 '11 at 3:15
@Caramdir: TikZ solution is still long: \tikz[baseline]\node[anchor=base,draw=red]{hello}; –  Leo Liu Jul 13 '11 at 5:00

Would setting the color before a framebox and resetting it to the normal text color inside and after the framebox work for your application?

\documentclass{article}
\usepackage{xcolor}

\begin{document}
\newcommand{\cfbox}[2]{%
\colorlet{currentcolor}{.}%
{\color{#1}%
\fbox{\color{currentcolor}#2}}%
}

\cfbox{red}{In red box} Normal black text

\color{blue} A paragraph with blue text and \cfbox{orange}{some words} in an orange box.
\end{document}


-
Scoping can be used to smooth coding workflow. –  xport Jul 13 '11 at 2:29
Good point, thanks for the hint! –  Jake Jul 13 '11 at 2:32
What is the trailing {} for? Is it really necessary? –  xport Jul 13 '11 at 2:47
It was necessary before I incorporated your scoping suggestion =) Thanks again! –  Jake Jul 13 '11 at 2:49
Thank you. I was anticipating such a method but wasn't sure if it works OK with \fbox fully correctly. Apparently it does. As a further improvement you can wrap the whole macro content in a { } group to keep the \colorlet local. Also I might add an early \leavevmode because I twice had issues with \color if this wasn't used. However, it seems to be fine here, which is funny. Note that \textcolor{..}{..} is basically just {\leavevmode\color{..} ..}. –  Martin Scharrer Jul 13 '11 at 6:47

In the meantime I build this functionality into my adjustbox package. It is included in v0.5 from 2011/08/07. Simply use the cfbox=<color> key which also has more potential arguments as explain in the manual.

You can also define a shortcut macro for it. I'm using here Jake's example text for easier comparison:

\documentclass{article}
\usepackage{xcolor}

\begin{document}
\newcommand{\cfbox}[1]{%
}

\cfbox{red}{In red box} Normal black text

\color{blue} A paragraph with blue text and \cfbox{orange}{some words} in an orange box.
\end{document}

-
\documentclass{minimal}
\usepackage{xcolor}
\pagecolor{yellow!20}
\newcommand{\mybox}[2]{{\color{#1}\fbox{\normalcolor#2}}}
\begin{document}

Using \normalcolor wouldn't work in the second example I used in my answer: The text in the framebox wouldn't have the same color as that surrounding it. –  Jake Jul 13 '11 at 2:33
@Jake: Is it a feature that Martin wants to get? :-) just call \mybox{<anothercolor>}{\color{<surroundingcolor>}text} that is a usual behavior adopted by TeX. –  xport Jul 13 '11 at 2:41