32

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.

lockstep
  • 250,273
Martin Scharrer
  • 262,582

4 Answers4

37

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}

colored framebox

Jake
  • 232,450
  • Scoping can be used to smooth coding workflow. – Display Name Jul 13 '11 at 02:29
  • What is the trailing {} for? Is it really necessary? – Display Name Jul 13 '11 at 02:47
  • It was necessary before I incorporated your scoping suggestion =) Thanks again! – Jake Jul 13 '11 at 02: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 06:47
8

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}
\usepackage{adjustbox}[2011/08/07]

\begin{document}
\newcommand{\cfbox}[1]{%
    \adjustbox{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}
Martin Scharrer
  • 262,582
5
\documentclass{minimal}
\usepackage{xcolor}
\pagecolor{yellow!20}
\newcommand{\mybox}[2]{{\color{#1}\fbox{\normalcolor#2}}}
\begin{document}   
\mybox{blue}{Enclosed by a blue box} Return to the normal color
\end{document}

enter image description here

Display Name
  • 46,933
  • 2
    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 02: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. – Display Name Jul 13 '11 at 02:41
1

Another option with no particular advantage, except if 1) you already have loaded colortbl and 2) you don't care that the result is not 100% the same as fbox's.

\documentclass[]{article}
\usepackage{colortbl}
\begin{document}

text 1
\begin{tabular}{|c|}
\arrayrulecolor{red}\hline text2 \\\hline 
\end{tabular} \fbox{text3} text4 \\
text text text

\end{document}

fboxvstabular

alfC
  • 14,350