我正在使用书籍类编写文档。我有一个很长的章节标题,只能容纳两行。我使用自定义章节标题字体,第二行章节标题的对齐方式被黑框隐藏。
\documentclass[12pt, twoside]{book}
\usepackage{lipsum}
\usepackage{titlesec,calc,xcolor}
\usepackage{lmodern}
\titleformat{\chapter}[display]{\fontsize{18}{16}\usefont{OT1}{phv}{m}{n}\bfseries}{}{0pt}{
\raisebox{-5mm}[0pt][0pt]{%<<
\begin{minipage}{20mm}
\centering{\mdseries\footnotesize\normalsize\textsc{Chapter}}\\[5pt]%
\rule{10mm}{30mm}\hspace{-10mm}%
\raisebox{23mm}{\parbox{10mm}{\centering\LARGE\textcolor{white}{\thechapter}}}%
\end{minipage}
}%
\rule{\textwidth-20mm}{.4pt}\\%
\hspace*{20mm}
}[\vspace*{10mm}]
\begin{document}
\chapter{This is an example of a very long title that only fits in two lines}
\section{Section 1}
\lipsum[1]
\end{document}
请问我该如何做?
答案1
您可以将章节标题放在 中minipage
。
\documentclass[12pt, twoside]{book}
\usepackage{lipsum}
\usepackage{titlesec,calc,xcolor}
\usepackage{lmodern}
\titleformat{\chapter}[display]{\fontsize{18}{16}\usefont{OT1}{phv}{m}{n}\bfseries}{}{0pt}{
\raisebox{-5mm}[0pt][0pt]{%<<
\begin{minipage}{20mm}
\centering{\mdseries\footnotesize\normalsize\textsc{Chapter}}\\[5pt]%
\rule{10mm}{30mm}\hspace{-10mm}%
\raisebox{23mm}{\parbox{10mm}{\centering\LARGE\textcolor{white}{\thechapter}}}%
\end{minipage}
}%
\rule{\textwidth-22mm}{.4pt}\\%
\hspace*{22mm}\begin{minipage}{\textwidth-22mm}
}[\end{minipage}\vspace*{10mm}]
\begin{document}
\chapter{This is an example of a very long title that only fits in two lines}
\section{Section 1}
\lipsum[1]
\end{document}
答案2
这里有一个变体:我将章节样式改为block
,将章节标题放在\parbox
宽度为的中\linegoal
,它测量从当前点到右边距的长度,并使用\titlerule
,它只是填充从当前点到右边距的线。
\documentclass[12pt, twoside]{book}
\usepackage{lmodern}
\usepackage{lipsum}
\usepackage{calc,xcolor}
\usepackage{linegoal}
\usepackage{titlesec}%
\titleformat{\chapter}[block]{\fontsize{18}{16}\usefont{OT1}{phv}{m}{n}\bfseries}{}{0pt}{
\raisebox{-5mm}[0pt][0pt]{%<<
\begin{minipage}{20mm}
\centering{\mdseries\footnotesize\normalsize\textsc{Chapter}}\\[5pt]%
\medskip\rule{10mm}{30mm}\hspace{-10mm}%
\raisebox{23mm}{\parbox{10mm}{\centering\LARGE\textcolor{white}{\thechapter}}}%
\end{minipage}
}%
\titlerule\\%
\hspace*{\dimexpr20mm +0.5em}
\parbox{\linegoal} }[\vspace*{10mm}]
\begin{document}
\chapter{This is an example of a very long title that only fits in two lines}
\section{Section 1}
\lipsum[1]
\end{document}