在 Latex 中,有没有办法从长字符串中取出 n 个较短的字符串?假设我有类似的东西\title{A very long title}
,我想把它分成两部分,以便以后可以使用colorbox{red}{A very}
和\color{blue}{long title}
。断点应通过用户提供的距离来计算。
答案1
我认为你不需要任何特殊的命令\chapter
:
\chapter[The real long title]
{\textcolor{red}{The real}\\
\textcolor{green}{long title}}
就是你所需要的。你可以用新命令来抽象它:
\documentclass{book}
\usepackage{xcolor}
\usepackage{xparse}
\newcommand{\firstchapline}[1]{\textcolor{red}{#1}}
\newcommand{\secondchapline}[1]{\textcolor{green}{#1}}
\makeatletter
\NewDocumentCommand{\coloredchapter}{som}
{\IfBooleanTF{#1}
{\chapter*{#3}}
{\IfNoValueTF{#2}
{\make@coloredchapter{#3}}
{\chapter[#2]{#3}}%
}%
}
\newcommand{\make@coloredchapter}[1]{%
\begingroup
\let\firstchapline\@firstofone
\let\secondchapline\@firstofone
\let\\\space
\protected@edef\x{\endgroup\noexpand\chapter[#1]}%
\x{#1}}
\makeatletter
\begin{document}
\frontmatter
\tableofcontents
\mainmatter
\coloredchapter{\firstchapline{A really}\\\secondchapline{long title}}
\coloredchapter[Short title]{\firstchapline{A really}\\\secondchapline{long title}}
\coloredchapter*{\firstchapline{A really}\\\secondchapline{long title}}
\end{document}