选择

选择

我使用了自己找到的一个部分样式这里问题是,尽管我尝试为未编号的部分创建单独的样式,但参考书目的标题仍然变成了一个框。

\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage{xcolor}
\usepackage[explicit]{titlesec}
\usepackage{soul}

\definecolor{titleblue}{HTML}{4a7aa4}

\title{Sections and Chapters}
\author{Educ}
\date{\today}

\newbox\TitleUnderlineTestBox
\newcommand*\TitleUnderline[1]
  {%
    \bgroup
    \setbox\TitleUnderlineTestBox\hbox{\colorbox{titleblue}\strut}%
    \setul{\dimexpr\dp\TitleUnderlineTestBox-.3ex\relax}{.3ex}%
    \ul{#1}%
    \egroup
  }
\newcommand*\SectionNumberBox[1]
  {%
    \colorbox{titleblue}
      {%
        \makebox[2.5em][c]
          {%
            \color{white}%
            \strut
            \csname the#1\endcsname
          }%
      }%
    \TitleUnderline{\ \ \ }%
  }
\titleformat{\section}
  {\Large\bfseries\sffamily\color{titleblue}}
  {\SectionNumberBox{section}}
  {0pt}
  {\TitleUnderline{#1}}
\titleformat{\subsection}
  {\large\bfseries\sffamily\color{titleblue}}
  {\SectionNumberBox{subsection}}
  {0pt}
  {\TitleUnderline{#1}}

\begin{document}

\maketitle

\section{The First Section}

\begin{thebibliography}{}
\bibitem{}
\end{thebibliography}


\end{document}

编辑:这是显示的错误消息:

我遇到了用组括号括起来的可连字符材料,我无法处理。要么删除括号,要么使用 \mbox (\hbox) 使材料无法断开。请注意,空格也算作可能的连字符点。请参阅手册第 4 页。我留下了一个黑色方块,这样你就能看到我现在的位置。

答案1

问题似乎是部分名称未扩展(如果我没记错的话)。以下使用扩展完全扩展了给定的名称\edef(这可能会产生新的问题,我不确定)。但是它适用于您的 MWE:

\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage{xcolor}
\usepackage[explicit]{titlesec}
\usepackage{soul}

\definecolor{titleblue}{HTML}{4a7aa4}

\title{Sections and Chapters}
\author{Educ}
\date{\today}

\newbox\TitleUnderlineTestBox
\newcommand*\TitleUnderline[1]
  {%
    \bgroup
    \setbox\TitleUnderlineTestBox\hbox{\colorbox{titleblue}\strut}%
    \setul{\dimexpr\dp\TitleUnderlineTestBox-.3ex\relax}{.3ex}%
    \edef\mytmp{#1}%
    \expandafter\ul\expandafter{\mytmp}%
    \egroup
  }
\newcommand*\SectionNumberBox[1]
  {%
    \colorbox{titleblue}
      {%
        \makebox[2.5em][c]
          {%
            \color{white}%
            \strut
            \csname the#1\endcsname
          }%
      }%
    \TitleUnderline{\ \ \ }%
  }
\titleformat{\section}
  {\Large\bfseries\sffamily\color{titleblue}}
  {\SectionNumberBox{section}}
  {0pt}
  {\TitleUnderline{#1}}
\titleformat{\subsection}
  {\large\bfseries\sffamily\color{titleblue}}
  {\SectionNumberBox{subsection}}
  {0pt}
  {\TitleUnderline{#1}}

\begin{document}

\maketitle

\section{The First Section}

\begin{thebibliography}{}
\bibitem{}
\end{thebibliography}

\end{document}

选择

下面定义了另一种样式,将未编号部分格式化为与编号部分类似的样式(它使用report和也定义\chapter为匹配,只需删除它就可以了article)。它还通过定义简写宏来消除对每个部分级别调用的不必要重复\titleformat

\documentclass{report}
\usepackage[utf8]{inputenc}
\usepackage{xcolor}
\usepackage[explicit]{titlesec}
\usepackage{soul}

\definecolor{titleblue}{HTML}{4a7aa4}

\title{Sections and Chapters}
\author{Educ}
\date{\today}

\newlength\SectionNumberBoxPad
\newcommand*\TitleUnderline[1]
  {%
    \begingroup
      \setul{\dimexpr\dp\strutbox+\fboxsep-.3ex\relax}{.3ex}%
      \edef\mytmp{#1}%
      \expandafter\ul\expandafter{\mytmp}%
    \endgroup
  }
\newcommand*\SectionNumberBoxSep{\TitleUnderline{\ \ \ }}
\newcommand*\UnnumberedSectionNumberBox
  {%
    \colorbox{titleblue}
      {\strut\hspace{\dimexpr\ht\strutbox+\dp\strutbox}}%
    \SectionNumberBoxSep
  }
\newcommand*\SectionNumberBox[1]
  {%
    \colorbox{titleblue}
      {%
        \begingroup
        \color{white}%
        \setlength\SectionNumberBoxPad{.5\dimexpr\ht\strutbox+\dp\strutbox}%
        \hspace{\SectionNumberBoxPad}%
        \strut\csname the#1\endcsname
        \hspace{\SectionNumberBoxPad}%
        \endgroup
      }%
    \SectionNumberBoxSep
  }
\makeatletter
\newcommand*\setuptitleformat[2]
  {%
    \expandafter\setuptitleformat@\csname #1\endcsname{#1}{#2}%
  }
\newcommand*\setuptitleformat@[3]
  {%
    \titleformat{#1}
      {#3\bfseries\sffamily\color{titleblue}}
      {\SectionNumberBox{#2}}
      {0pt}
      {\TitleUnderline{##1}}%
    \titleformat{name=#1,numberless}
      {#3\bfseries\sffamily\color{titleblue}}
      {\UnnumberedSectionNumberBox}
      {0pt}
      {\TitleUnderline{##1}}%
  }
\makeatother
\setuptitleformat{chapter}{\LARGE}
\setuptitleformat{section}{\Large}
\setuptitleformat{subsection}{\large}
\setuptitleformat{subsubsection}{}

\setcounter{secnumdepth}{3}
\setcounter{chapter}{10}
\begin{document}

\maketitle

\chapter{The First Chapter}
\section{The First Section}
\subsection{The First Subsection}
\subsubsection{The First Subsubsection}
Some text

\begin{thebibliography}{}
\bibitem{}
\end{thebibliography}

\end{document}

在此处输入图片描述


方案 2

此版本在打印时应该看起来相同,但 PDF 的显示取决于查看器(尽管 xpdf 是可信的;这是因为使用的ulem线条是使用小段构建的,这可能会导致屏幕上出现锯齿问题)。它会破坏自动连字,但仍然可以换行。它使用包ulem而不是包soululem应该更强大,因此\edef不需要对其进行扩展。

\documentclass{report}
\usepackage[utf8]{inputenc}
\usepackage{xcolor}
\usepackage[explicit]{titlesec}
\usepackage[normalem]{ulem}

\definecolor{titleblue}{HTML}{4a7aa4}

\title{Sections and Chapters}
\author{Educ}
\date{\today}

\newlength\SectionNumberBoxPad
\newlength\TitleUnderlineSeg
\setlength\TitleUnderlineSeg{.05pt}
\newcommand*\TitleUnderline[1]
  {%
    \bgroup
    \markoverwith
      {%
        \rule
          [-\dimexpr\dp\strutbox+\fboxsep\relax]
          {\TitleUnderlineSeg}
          {.3ex}%
      }%
    \ULon{#1}%
  }
\newcommand*\SectionNumberBoxSep
  {%
    \hspace{-\TitleUnderlineSeg}%
    \TitleUnderline
      {\hspace{\TitleUnderlineSeg}\hspace{\SectionNumberBoxPad}}%
  }
\newcommand*\UnnumberedSectionNumberBox
  {%
    \begingroup
      \setlength\SectionNumberBoxPad{.5\dimexpr\ht\strutbox+\dp\strutbox}%
      \colorbox{titleblue}
        {\strut\hspace{2\SectionNumberBoxPad}}%
      \SectionNumberBoxSep
    \endgroup
  }
\newcommand*\SectionNumberBox[1]
  {%
    \begingroup
      \setlength\SectionNumberBoxPad{.5\dimexpr\ht\strutbox+\dp\strutbox}%
      \colorbox{titleblue}
        {%
          \begingroup
          \color{white}%
          \hspace{\SectionNumberBoxPad}%
          \strut\csname the#1\endcsname
          \hspace{\SectionNumberBoxPad}%
          \endgroup
        }%
      \SectionNumberBoxSep
    \endgroup
  }
\makeatletter
\newcommand*\setuptitleformat[2]
  {%
    \expandafter\setuptitleformat@\csname #1\endcsname{#1}{#2}%
  }
\newcommand*\setuptitleformat@[3]
  {%
    \titleformat{#1}
      {#3\bfseries\sffamily\color{titleblue}}
      {\SectionNumberBox{#2}}
      {0pt}
      {\TitleUnderline{##1}}%
    \titleformat{name=#1,numberless}
      {#3\bfseries\sffamily\color{titleblue}}
      {\UnnumberedSectionNumberBox}
      {0pt}
      {\TitleUnderline{##1}}%
  }
\makeatother
\setuptitleformat{chapter}{\LARGE}
\setuptitleformat{section}{\Large}
\setuptitleformat{subsection}{\large}
\setuptitleformat{subsubsection}{}

\setcounter{secnumdepth}{3}
\setcounter{chapter}{10}
\begin{document}

\maketitle

\chapter{The First Chapter}
\section{The First Section}
\subsection{The First Subsection}
\subsubsection{The First Subsubsection}
Some text
\section
  {Really long section name that is really long, so long it takes two rows}

\begin{thebibliography}{}
\bibitem{}
\end{thebibliography}

\end{document}

在此处输入图片描述

这些线确实与部分编号的框垂直对齐(以xpdf2000% 放大率检查),不匹配是由于 Gimp 的光栅化造成的。

相关内容