为标题提供横跨整个字体区域的背景颜色

为标题提供横跨整个字体区域的背景颜色

我发布于堆栈溢出然后被重定向到这里。这里确实看起来是一个更好的询问地点。

我想通过添加背景颜色来装饰我的标题(部分、子部分等)。我希望此框填充单一纯色以横跨整个类型区域。

这张图片是 OpenOffice 文档的屏幕截图,其中标题具有我想要的格式、布局和颜色。

Image showing example

解决方案要求

  1. 生成的样式应该被其他人使用,这意味着应该通过以下代码创建与上图等效的乳胶输出:

    Some Text Some Text Some Text Some Text Some Text Some Text Some Text
    Some Text Some Text Some Text Some Text Some Text Some Text Some Text
    Some Text Some Text Some Text Some Text Some Text Some Text Some Text
    Some Text Some Text Some Text Some Text Some Text Some Text Some Text
    Some Text Some Text Some Text Some Text Some Text Some Text Some Text
    Some Text Some Text Some Text Some Text Some Text 
    
    \subsubsection{This is a headline}
    
    Some Text Some Text Some Text Some Text Some Text Some Text Some Text
    Some Text Some Text Some Text Some Text Some Text Some Text Some Text
    Some Text Some Text Some Text Some Text Some Text Some Text Some Text
    Some Text Some Text Some Text Some Text Some Text Some Text Some Text
    Some Text Some Text Some Text Some Text Some Text Some Text Some Text
    Some Text Some Text Some Text Some Text Some Text
    
  2. 如果该部分不应该被编号,那么应该使用\subsubsection*

  3. LaTeX 仍然需要负责段落和蓝色框之间的空白。

到目前为止我尝试过

据我所知,标题和章节编号是一个简单的段落。Latex 在前后添加了一些跳过,并挤进了一些修改字体的命令。

我的想法是将这个段落锁定在 中\colorbox。colorbox 宏的工作方式如下:\colorbox{color}{the content around which to draw a box filled with the specified color}。我的问题是我无法掌握这个段落。

轻松的 80%

我的作品基于 koma-script,并通过重新定义\othersectionlevelsformat\(sub)*section。我可以绘制两个框,一个框围绕章节编号,另一个框围绕章节标题。然而,这有 3 个未解决的问题:

  1. 框的上边缘未对齐,且标题框比编号框稍大
  2. 标题周围的框未延伸到类型区域的右端
  3. 如果标题跨越多行,则编号周围的框不会增大(这并不奇怪)

困难的20%

我尝试让 latex 将章节编号放入\savebox稍后可以访问的格式中,并将其作为标题的一部分。这样就无需使用两个框,同时解决了我的所有三个问题,但这并不奏效。我只能修改标题实际编号已完成。因此,虽然我可以将编号放入保存框,但我无法对其进行任何操作,直到为时已晚。

我尝试将\section ...语句放入 colorbox,但结果却不是这样,文档无法编译。savebox 也是! LaTeX Error: Something's wrong--perhaps a missing \item.如此。我猜 colorbox 会使用它们。

我尝试将\section ...声明放在小页面中,并在其周围放置一个框。这有两个问题。第一:Latex 不会在节上方留出任何空白。第二:不仅节编号和标题有背景,而且节标题和第一段之间的空白也有背景。

那么,怎样才能破解乳胶,使之完成如上图所示的章节标题格式化呢?

答案1

这是我第二次尝试。它使用与 Sharpie 相同的想法,但不需要 tikz。

\documentclass{scrartcl}

\usepackage{xcolor}
\usepackage{lipsum}

\setkomafont{section}{\mysection}
\newcommand{\mysection}[1]{%
    \Large\sf\bf%
    \setlength{\fboxsep}{0cm}%already boxed
    \colorbox{orange!80}{%
        \begin{minipage}{\linewidth}%
            \vspace*{2pt}%Space before
            #1
            \vspace*{2pt}%Space after
        \end{minipage}%
    }}

    \begin{document}

        \section{Example section}
        \lipsum[1]

        \section*{A very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very long section}
        \lipsum[1]
\end{document}

结果是: result 请随意采用小节等;)

答案2

以下解决方案使用\titleformat命令titlesec包裹,参数中的最后一个命令before-code\colorsection)用于将标题包装在\colorbox(在color包裹) 更改背景颜色。

这是针对一个部分,但您可以使用类似的方案轻松地将其扩展为子部分:

\documentclass[a4paper]{scrbook}
\usepackage{xcolor,lipsum}
\usepackage{titlesec}

\titleformat{name=\section}[block]
  {\sffamily\large}
  {}
  {0pt}
  {\colorsection}
\titlespacing*{\section}{0pt}{\baselineskip}{\baselineskip}

\newcommand{\colorsection}[1]{%
  \colorbox{blue!20}{\parbox{\dimexpr\textwidth-2\fboxsep}{\thesection\ #1}}}

\begin{document}

\lipsum[1]
\section{This is the title}
\lipsum[2]
\end{document}

如果想要在彩色文本上显示白色,只需修改命令\colorsection,例如

\newcommand{\colorsection}[1]{%
  \colorbox{blue}{\parbox{\dimexpr\textwidth-2\fboxsep}{\color{white}\thesection\ #1}}}

enter image description here

答案3

以下是重新定义新命令的建议\sectionlinesformat,该命令在文档版本 3.19 或更新版本:

\documentclass{scrartcl}[2015/09/15]
\usepackage{blindtext}% dummy text
\usepackage{xcolor}
\colorlet{sectioncolor}{orange}
\makeatletter
\renewcommand\sectionlinesformat[4]{%
  \colorbox{sectioncolor}{%
    \parbox[t]{\dimexpr\textwidth-2\fboxsep\relax}{%
      \raggedsection\color{white}\@hangfrom{#3}{#4}%
}}}
\makeatother
%\renewcommand\raggedsection{\centering}
\begin{document}
  \section{Example section}
  \blindtext
  \section{A very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very long section}
  \blindtext
  \addsec{A very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very long section}
  \blindtext
  \blinddocument
\end{document}

enter image description here

还可以对节、小节和小小节使用不同的颜色:

\documentclass{scrartcl}[2015/09/15]
\usepackage{blindtext}% dummy text
\usepackage{xcolor}
\colorlet{sectioncolor}{orange}
\colorlet{subsectioncolor}{purple}
\colorlet{subsubsectioncolor}{blue!50!black}
\makeatletter
\renewcommand\sectionlinesformat[4]{%
  \colorbox{#1color}{%
    \parbox[t]{\dimexpr\textwidth-2\fboxsep\relax}{%
      \raggedsection\color{white}\@hangfrom{#3}{#4}%
}}}
\makeatother
%\renewcommand\raggedsection{\centering}
\begin{document}
  \section{Example section}
  \blindtext
  \section{A very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very long section}
  \blindtext
  \addsec{A very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very long section}
  \blindtext
  \blinddocument
\end{document}

enter image description here


的第二个参数 ( #2)\sectionlinesformatindent您可以使用 设置的节标题的\RedeclarSectionCommand。以下是使用负缩进将节标题放大到左边距的示例:

\documentclass{scrartcl}[2015/09/15]
\usepackage{blindtext}% dummy text
\usepackage{xcolor}
\colorlet{sectioncolor}{orange}
\colorlet{subsectioncolor}{purple}
\colorlet{subsubsectioncolor}{blue!50!black}
\makeatletter
\renewcommand\sectionlinesformat[4]{%
  \hspace{#2}%
  \colorbox{#1color}{%
    \parbox[t]{\dimexpr\textwidth-2\fboxsep-#2\relax}{%
      \raggedsection\color{white}\@hangfrom{#3}{#4}%
}}}
\makeatother
\RedeclareSectionCommand[indent=-1em]{section}
%\renewcommand\raggedsection{\centering}
\begin{document}
  \section{Example section}
  \blindtext
  \section{A very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very long section}
  \blindtext
  \addsec{A very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very long section}
  \blindtext
  \blinddocument
\end{document}

enter image description here

答案4

这是另一种方法,它利用了将一个参数传递给 的宏的行为\setkomafont。实际的样式是使用 TikZ 节点完成的。

\documentclass{scrartcl}

\usepackage{tikz}
\usepackage{lipsum}

\tikzset{
  % Styling of header text is done using key/value options for TikZ nodes. See
  % section 16.4 of the PGF manual for a complete list of options that affect
  % text.
  headings/base/.style = {
    % Zap node seperation, set text width and alignment.
    outer sep = 0pt,
    % Trim off 2/3rd of an em to compensate for the inner xsep which spaces the
    % text nicely away from the left side, but causes the node to hang into the
    % right margin.
    text width = {\textwidth - 0.6666em},
    align = left,
    text = white,
  },
  headings/section/.style = {
    headings/base,
    fill = blue!50,
    font = \sffamily\Large
  },
  headings/subsection/.style = {
    headings/base,
    fill = blue!25,
    font = \sffamily\large
  }
}

\newcommand{\colorboxedsec}[2]{%
  \tikz{\node[headings/#1]{#2};}}

\setkomafont{section}{\colorboxedsec{section}}
\setkomafont{subsection}{\colorboxedsec{subsection}}


\begin{document}

\section{Lorem Ipsum}
  \lipsum[1]

\subsection{History of Lorem Ipsum}
  Foo bar baz.

\section*{A section header full of \lipsum[1]}

\end{document}

结果:

enter image description here

相关内容