latex3 / xcolor

Driver-independent color extensions for LaTeX and pdfLaTeX
LaTeX Project Public License v1.3c
28 stars 5 forks source link

colored frame with no background #28

Closed Udi-Fogiel closed 1 year ago

Udi-Fogiel commented 1 year ago

It would be nice to have a version of \fcolorbox that does not have background. For example we can define with something similar to

\documentclass{article}

\usepackage{xcolor}
\makeatletter

\newcommand*\XC@cframeb@x[1]%
{\hbox
    {\@tempdima\fboxrule \advance\@tempdima\fboxsep
        \advance\@tempdima\dp\@tempboxa
        \setbox\@tempboxa\hbox
        {\lower\@tempdima\hbox
            {\vbox
                {\kern\fboxrule
                    \hbox{\kern\fboxrule
                        \vbox{\kern\fboxsep\box\@tempboxa\kern\fboxsep}%
                        \kern\fboxrule}%
                    \kern\fboxrule}}}%
        \@tempdima\wd\@tempboxa
        \edef\@tempa{{\the\@tempdima}{\the\ht\@tempboxa}{\the\dp\@tempboxa}{#1}}%
        \box\@tempboxa\kern-\@tempdima\expandafter\boxcframe\@tempa}}

\newcommand*\boxcframe[4]%
{\hbox
    {\dimen@#2\advance\dimen@#3\relax
        \lower#3\vbox
        {{\color{#4}\hrule\@height\fboxrule}%
            \@tempdima-0.5\fboxrule \ifodd\fboxrule\advance\@tempdima\m@ne sp\fi
            \kern\@tempdima
            \hbox
            {\advance\dimen@-\fboxrule
                {\color{blue}\vrule\@width\fboxrule\@height\dimen@\@depth\z@}%
                \@tempdima#1\advance\@tempdima-\tw@\fboxrule \kern\@tempdima
                {\color{#4}\vrule\@width\fboxrule\@height\dimen@\@depth\z@}}%
            \kern-0.5\fboxrule
            {\color{#4}\hrule\@height\fboxrule}}}}

\DeclareRobustCommand\cfbox[2]{%
    \leavevmode
    \setbox\@tempboxa\hbox{%
        \color@begingroup
        \kern\fboxsep{#2}\kern\fboxsep
        \color@endgroup}%
    \XC@cframeb@x{#1}}

\begin{document}
    \cfbox{blue}{Hello}
\end{document}

This is just an example, of curse it can be somehow use the already existing commands with some hack to shorten the code...

muzimuzhi commented 1 year ago

fbox package provides a similar flexibility, by redefining \fbox.

\documentclass{article}
\usepackage{fbox}
% \usepackage{xcolor} % already loaded by fbox

\makeatletter
% set color for all lines. Default "black"
\define@key{fbox}{framecolor}[black]{%
  \def\fbox@bcolor{#1}\def\fbox@tcolor{#1}%
  \def\fbox@lcolor{#1}\def\fbox@rcolor{#1}}
\makeatother

\def\offset{\hskip\dimexpr\fboxsep+\fboxrule\relax}

\begin{document}
content content\offset\llap{\fbox{abc abc}}

content content\offset\llap{\fbox[framecolor=blue]{abc abc}}

content content\offset\llap{\fcolorbox{blue!20}{red!20}{abc abc}}
\end{document}

image

Udi-Fogiel commented 1 year ago

Such a simple package, only about 100 lines! I love it. Thanks!