我使用了我找到的剖面樣式這裡。問題是,儘管我嘗試為未編號的部分創建單獨的樣式,但參考書目的標題變成了一個框。
\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
包而不是soul
包。ulem
應該更強大,因此\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}
這些線確實與部分編號框垂直對齊(xpdf
在 2000% 放大倍率下檢查),不匹配是由於 Gimp 的光柵化造成的。