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

share|improve this question
2  
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

3 Answers 3

up vote 17 down vote accepted

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

share|improve this answer
    
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}
\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}
share|improve this answer
\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

share|improve this answer
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 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

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.