我已经创建了许多以下自定义框,但我确信有更好的方法来实现相同的结果:我想使用在使用时调用参数的东西,例如:
\myuniversalbox[background_color=, frame_color=, borderlinewidth=, arc_radius=, separation_x=, separation_y=]{some text}.
如何定义颜色等方法不是问题;问题在于如何在 .tex 文件中设置整个内容。
%------------------------- %
\documentclass{book}
%------------------------- %
\usepackage[english]{babel} %
\usepackage{newtxtext} %
\usepackage{colortbl}
\usepackage{graphicx} %
%
\setlength{\unitlength}{1mm}
%
\definecolor{DarkRed}{rgb}{0.45,0.00,0.00}
\definecolor{LightGrey}{rgb}{0.96,0.96,0.96}
\definecolor{LightPink}{rgb}{1.00,0.92,0.92}
\definecolor{LightYellow}{rgb}{1.00,1.00,0.85}
\definecolor{PaleBlue}{rgb}{0.92,1.00,1.00}
\newcommand{\redpaleblueboxtwofour}[1]{ %
\setlength{\fboxrule}{1.25pt}
{\setlength{\fboxsep}{2.4pt}
\hspace{-4pt}\fcolorbox{DarkRed}{PaleBlue}{#1}}}
\newcommand{\redlightyellowboxsixeight}[1]{ %
\setlength{\fboxrule}{1.25pt}
{\setlength{\fboxsep}{6.8pt}
\hspace{-4pt}\fcolorbox{DarkRed}{LightYellow}{#1}}}
\newcommand{\redlightpinkthreesix}[1]{ %
\setlength{\fboxrule}{0.75pt}
{\setlength{\fboxsep}{3.6pt}
\hspace{-4pt}\fcolorbox{DarkRed}{LightPink}{#1}}}
\newcommand{\redlightgreyboxfoureight}[1]{ %
\setlength{\fboxrule}{0.75pt}
{\setlength{\fboxsep}{4.8pt}
\hspace{-4pt}\fcolorbox{DarkRed}{LightGrey}{#1}}}
\begin{document}
\noindent \hspace{50pt}\fbox{Some ordinary text}
\vspace{4pt}
\noindent \hspace{50pt}\redpaleblueboxtwofour{Some ordinary text}
\vspace{4pt}
\noindent \hspace{50pt}\redlightyellowboxsixeight{Some ordinary text}
\vspace{4pt}
\noindent \hspace{50pt}\redlightpinkthreesix{Some ordinary text}
\vspace{4pt}
\noindent \hspace{50pt}\redlightgreyboxfoureight{Some ordinary text $+\hspace{1.2pt}x^{\hspace{0.6pt}2}$}
\end{document}
答案1
另外两种方法可以实现此目的:
一种方法是使用其中一个键值选项包。下面的代码中pgfkeys
使用了该选项。这不会像您的示例所建议的那样,使文本的左右角变圆或距离不同。
因此,另一种方法是使用tcolorbox
包,它可以做你想做的一切,甚至更多。下面代码中的示例仅仅触及了它功能的表面。
%------------------------- %
\documentclass{book}
%------------------------- %
\usepackage[english]{babel} %
%\usepackage{newtxtext} %
\usepackage{colortbl}
\usepackage{graphicx} %
\usepackage{xcolor}
\usepackage{pgfkeys}
\usepackage{tcolorbox}
%
\setlength{\unitlength}{1mm}
%
\definecolor{DarkRed}{rgb}{0.45,0.00,0.00}
\definecolor{LightGrey}{rgb}{0.96,0.96,0.96}
\definecolor{LightPink}{rgb}{1.00,0.92,0.92}
\definecolor{LightYellow}{rgb}{1.00,1.00,0.85}
\definecolor{PaleBlue}{rgb}{0.92,1.00,1.00}
% define the options
\pgfkeys{%
/mybox/.is family, /mybox/.cd,
background color/.code={\colorlet{bgcolor}{#1}},
frame color/.code={\colorlet{framecolor}{#1}},
frame sep/.code={\setlength{\fboxsep}{#1}},
frame width/.code={\setlength{\fboxrule}{#1}},
% initialize colors
background color=white,
frame color=black
}
% universal box
\newcommand{\mybox}[2][]{%
\begingroup
\pgfkeys{mybox,#1}%
\fcolorbox{framecolor}{bgcolor}{#2}%
\endgroup
}
\newcommand{\redpaleblueboxtwofour}[1]{%
\mybox[frame width=1.25pt,
frame sep=2.4pt,
frame color=DarkRed,
background color=PaleBlue]{#1}}
\newcommand{\redlightyellowboxsixeight}[1]{%
\mybox[frame width=1.25pt,
frame sep=6.8pt,
frame color=DarkRed,
background color=LightYellow]{#1}}
\newcommand{\redlightpinkthreesix}[1]{%
\mybox[frame width=0.75pt,
frame sep=3.6pt,
frame color=DarkRed,
background color=LightPink]{#1}}
\newcommand{\redlightgreyboxfoureight}[1]{%
\mybox[frame width=0.75pt,
frame sep=4.8pt,
frame color=DarkRed,
background color=LightGrey]{#1}}
\begin{document}
\noindent \fbox{Some ordinary text}
\vspace{4pt}
\noindent \redpaleblueboxtwofour{Some ordinary text}
\vspace{4pt}
\noindent \redlightyellowboxsixeight{Some ordinary text}
\vspace{4pt}
\noindent \redlightpinkthreesix{Some ordinary text}
\vspace{4pt}
\noindent \redlightgreyboxfoureight{Some ordinary text $+\hspace{1.2pt}x^{\hspace{0.6pt}2}$}
\vspace{4pt}
\noindent \mybox{Some Text}
\vspace{4pt}
\noindent \mybox[frame width=5pt,frame sep=10pt,frame color=blue,background color=cyan]{Some Text}
\vspace{20pt}
\noindent With \texttt{tcolorbox} and global settings\\
\texttt{nobeforeafter,tcbox raise base,colframe=DarkRed,colback=PaleBlue}
\tcbset{nobeforeafter,tcbox raise base,colframe=DarkRed,colback=PaleBlue}% set some things globally
\vspace{4pt}
\noindent \tcbox{Some other Text}
\vspace{4pt}
\noindent \tcbox[arc=2mm]{Some other Text} \texttt{arc=2mm}
\vspace{4pt}
\noindent \tcbox[sharp corners]{Some other Text} \texttt{sharp corners}
\vspace{4pt}
\noindent \tcbox[boxsep=2mm]{Some other Text} \texttt{boxsep=2mm}
\vspace{4pt}
\noindent \tcbox[boxrule=2mm]{Some other Text} \texttt{boxrule=2mm}
\end{document}
答案2
这是一个 LaTeX3 解决方案,它处理盒子的各种参数,然后将它们放到位,以便
\UniversalBox{With defaults}
\UniversalBox[background color=LightYellow]{Yellow background}
\UniversalBox[background color=PaleBlue, borderline width=2pt,
frame color=DarkRed]{More text}
\UniversalBox{More text}
生产
以下是完整代码
%------------------------- %
\documentclass{book}
%------------------------- %
\usepackage[english]{babel} %
\usepackage{newtxtext} %
\usepackage[svgnames]{xcolor}
\usepackage{graphicx} %
\usepackage{xparse}
%
\setlength{\unitlength}{1mm}
\definecolor{DarkRed}{rgb}{0.45,0.00,0.00}
\definecolor{LightGrey}{rgb}{0.96,0.96,0.96}
\definecolor{LightPink}{rgb}{1.00,0.92,0.92}
\definecolor{LightYellow}{rgb}{1.00,1.00,0.85}
\definecolor{PaleBlue}{rgb}{0.92,1.00,1.00}
\ExplSyntaxOn
\keys_define:nn {universalbox}
{
arc radius .dim_set:N = \l_univbox_arc_radius_dim,
background color .tl_set:N = \l_univbox_background_color_tl,
borderline width .dim_set:N = \l_univbox_borderlinewidth_dim,
frame color .tl_set:N = \l_univbox_frame_color_tl,
separation x .dim_set:N = \l_univbox_separation_x_dim,
separation y .dim_set:N = \l_univbox_separation_y_dim,
arc radius .initial:n = 1pt, % default values
background color .initial:n = White,
borderline width .initial:n = 0.5pt,
frame color .initial:n = Black,
separation x .initial:n = 2pt,
separation y .initial:n = 2pt
}
% \UniversalBox[optional box settings]{text}
\NewDocumentCommand\UniversalBox{ O{} m }{
% start a group to keep key setting local
\group_begin:
\keys_set:nn{universalbox}{#1}
\setlength {\fboxrule} {\l_univbox_borderlinewidth_dim}
\setlength {\fboxsep} {\l_univbox_separation_x_dim}
\hspace {\l_univbox_separation_y_dim}
\fcolorbox{\l_univbox_frame_color_tl}{\l_univbox_background_color_tl}{#2}
\group_end:% close group
}
\ExplSyntaxOff
\begin{document}
\UniversalBox{With defaults}
\UniversalBox[background color=LightYellow]{Yellow background}
\UniversalBox[background color=PaleBlue, borderline width=2pt,
frame color=DarkRed]{More text}
\UniversalBox{More text}
\end{document}
对于那些知道如何使用 expl3 的人,我深表歉意。请随意纠正!