考虑@egreg 给出的以下答案,可以在以下位置找到:https://tex.stackexchange.com/a/111043/10898
为什么在发出以下命令时会创建额外的 6.799999pt 空间
\framecolorbox[\textwidth]{blue}{blue!40}{what do I do here?}
这和帧分离有关吗?一个简单的解决方法是发出如下命令
\framecolorbox[\dimexpr\textwidth-6.799999pt\relax]{blue}{blue!40}{what do I do here?}
但这个答案似乎太过简单了。
答案1
我建议使用不同的命令,比如说\framecolorbox*
,当不应该考虑填充和规则厚度时。
\documentclass{article}
\usepackage{xcolor,xparse}
\ExplSyntaxOn
\NewDocumentCommand{\framecolorbox}{soommm}
{% #1 = star
% #2 = width (optional)
% #3 = inner alignment (optional)
% #4 = frame color
% #5 = background color
% #6 = text
\IfValueTF{#2}
{
\IfValueTF{#3}
{
\fcolorbox{#4}{#5}{
\makebox[
\dim_eval:n {#2 \IfBooleanT{#1}{- 2\fboxsep - 2\fboxrule}}
][#3]{#6}
}
}
{
\fcolorbox{#4}{#5}{\makebox[
\dim_eval:n { #2 \IfBooleanT{#1}{- 2\fboxsep - 2\fboxrule}}
]{#6}
}
}
}
{\fcolorbox{#4}{#5}{#6}}
}
\ExplSyntaxOff
\begin{document}
\noindent X\hrulefill X
\noindent\framecolorbox*[\textwidth]{blue}{blue!40}{what do I do here?}
\noindent\framecolorbox*[\textwidth][l]{blue}{blue!40}{what do I do here?}
\noindent\framecolorbox[\textwidth][s]{blue}{blue!40}{what do I do here?}
\end{document}
答案2
稍微修改的例子,有一个只有 0.8 pt 的溢出框(归功于为什么 \textwidth 不是文本的宽度?),现在根本没有满箱(感谢下面评论中的 Mico):
\documentclass{article}
\usepackage{xcolor,xparse}
\usepackage{calc}
\NewDocumentCommand{\framecolorbox}{oommm}
{% #1 = width (optional)
% #2 = inner alignment (optional)
% #3 = frame color
% #4 = background color
% #5 = text
\IfValueTF{#1}%
{%
\IfValueTF{#2}%
{\fcolorbox{#3}{#4}{\makebox[#1 - 2\fboxsep - 2\fboxrule][#2]{#5}}}%
{\fcolorbox{#3}{#4}{\makebox[#1 - 2\fboxsep - 2\fboxrule]{#5}}}%
}%
{\fcolorbox{#3}{#4}{#5}}%
}%
\begin{document}
\noindent\framecolorbox[\textwidth]{blue}{blue!40}{what do I do here?}
\end{document}