在 LaTeX 中覆盖小节标题的背景颜色
这里是 LaTeX 新手。
我需要为所有 \subsection
标题设置背景颜色。整行应该改变颜色,而不仅仅是带有文本的部分。
这确实有效:
\subsection{\colorbox{Gray}{Title}}
但它不会为整条线着色。另外,我想在一个地方为所有 \subsections
配置它。
我的 google-fu 让我失望了。关于如何做我想做的事有什么建议吗?
LaTeX newbie here.
I need to set background color for all my \subsection
titles. Whole line should change color, not only the part of it with text.
This does work:
\subsection{\colorbox{Gray}{Title}}
But it does not color whole line. Also I'd like to configure it in a single place for all \subsections
.
My google-fu is failing me. Any suggestions on how to do what I want?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
要使
\colorbox
成为线条的宽度,请使用\makebox
:我不能 100% 确定“
\hfill
”就是您想要的需要放入第一组方括号中。您可能需要尝试该部分。另一种值得尝试的方法是要在一个地方为所有小节配置它,最简单的方法是定义一个包装器命令:
您也可以重新定义
\subsection
,但随后您必须了解内部命令它使用并注意匹配文档类的其他格式。我不推荐它。To make the
\colorbox
the width of the line, use\makebox
:I'm not 100% sure "
\hfill
" is what you need to put in the first set of square brackets. You may need to experiment with that part. An alternative worth trying isTo configure it in one place for all subsections, the easiest thing to do is define a wrapper command:
You could also redefine
\subsection
, but then you have to learn about the internal commands it uses and take care to match your documentclass's other formatting. I don't recommend it.扩展扎克的答案,这是我的解决方案:
如果您希望文本左对齐
或居中,
如果您使用的是全局设置,则可以删除 \setlength\fboxsep 的本地声明。
显然 bgcol 和 textcol 需要在文档序言中提前定义。
如果在 multicols 环境中,您可以使用 \textwidth 或 \columnwidth,或其中任一个的相对数量,具体取决于您如何布置内容以及您希望它们的外观。
我使用的多列文本占该列的 100%。但发现使用 \textwidth 或 \columnwidth 与下面文本正文的宽度相比,标题悬垂到右侧,因此为了纠正这个问题,我实际上最终使用了:
注意:我收到来自“Overfull \hbox”的错误消息使用此代码。我不知道如何解决这个问题,但输出工作正常,所以这对我来说不是问题。在不同的程序或构建上这可能会给您带来问题!
Expanding on Zack's answer, this is my solution:
If you want the text left aligned
or if you want it centred
You can drop the local declaration of \setlength\fboxsep if you are using a global setting.
Obviously bgcol and textcol need to be defined earlier in the document preamble.
If are in the multicols environment you can use \textwidth or \columnwidth, or a relative amount of either these, depending on how you have things laid out and how you want them to look.
I am using multicols with the text spanning 100% of the column. But found that using either \textwidth or \columnwidth the headings overhang to the right side compared with the width of the text body underneath, so to correct this I actually ended up using:
Caution: I am getting error messages from "Overfull \hbox" using this code. I don't know how to resolve this, but the output is working fine so it is not an issue for me. On a different program or build this may cause you problems!