LaTeX教程(007)-LaTeX文档结构(07)

LaTeX教程(007)- LaTeX \LaTeX LATEX文档结构(07)

扩展接口

扩展接口由两个主要命令组成,分别是\titleformat\titlespacing\titleformat用来设置标题内部的格式(字体、标签、对齐方式等),\titlespacing用来定义标题的外部格式(标题与外部内容之间的距离、缩进等)。

在讲这两个命令之前,我们先对一个完整标题的内部作出区分。对一个形如"第X章 LaTeX \LaTeX LATEX的文档结构"的标题,我们将" LaTeX \LaTeX LATEX的文档结构"称为标题文本,将"第X章"称为标头,其中,"X"是标题编号,"第()章"是标题的预定义文本。

我们首先介绍命令\titleformat。该命令的形式如下:

\titleformat{cmd}[shape]{format}{label}{sep}{before-code}[after-code]
  • 参数cmd是必选参数,是我们要修改的标题所对应的划分命令,如\section等。

  • 参数shape是可选参数,我们用它定义标题的基本形状。可以选择的参数如下:

    • hang是默认值,它产生一个悬挂标头,就像标准类中的\section
    • display产生一个陈列标头,即标头独占一行,就像标准类中的\chapter
    • runin产生一个内嵌标题,就像标准类中的\paragraph
    • 此外,titlesec包还提供了 LaTeX \LaTeX LATEX​​标准类中所没有的格式,如frame,使标头独占一行,并且在标题外面加了一个框。leftmargin将标题放在左边距,而rightmargin将标题放在右边距。这两个形状可能与\marginpar命令冲突。\marginpar命令的作用是将一段文本放在边距上,它可能会与这两个选项重叠。
    • block将完整的标题作为一个单独的块进行排版。对于居中布局的标题很有用。
    • dropwrap将标题嵌入到标题后的第一个段落文本中,drop宽度固定,wrap使用最宽标题行的宽度。

现在我们先用一个例子演示一下这shape参数的前三个选项:

\documentclass{book}
\usepackage[a5paper,margin=1in]{geometry}
\usepackage{titlesec}
\usepackage{kantlipsum}
\begin{document}
% 我们这里只关注前两个参数,后面的不用理解
\titleformat{\chapter}[hang]{\normalfont\bfseries}{\S\,\thesection.}{.5em}{}
% 将\chapter标题定义为标头与标题文本同行的格式
\titleformat{\section}[display]{\normalfont\bfseries}{\S\,\thesection.}{.5em}{}
% 将\section标题定义为标头独占一行的格式
\titleformat{\subsection}[runin]{\normalfont\bfseries}{\S\,\thesection.}{.5em}{}
% 将\subsection定义成内嵌标题
\chapter{A chapter}
\section{A section}
\subsection{A subsection}
\kant[1]
\end{document}

编译:

在这里插入图片描述

我们再演示一下后面的选项中的一部分:

\documentclass{book}
\usepackage[a5paper,margin=1in]{geometry}
\usepackage{titlesec}
\usepackage{kantlipsum}
\begin{document}
\titleformat{\chapter}[frame]{\normalfont\bfseries}{\S\,\thesection.}{0.5em}{}
\titleformat{\section}[wrap]{\normalfont\bfseries}{\S\,\thesection.}{0.5em}{}
\titleformat{\subsection}[rightmargin]{\normalfont\bfseries}{\S\,\thesection.}{.5em}{}
\chapter{A chapter}
\section{A section}
\kant[1]
\subsection{A subsection}
\kant[2]
\end{document}

编译:

在这里插入图片描述

  • 参数format是必选参数,它可以是一个垂直材料,也可以是一些样式设置命令。这些内容会在标题的正上方的空间后生效。因此,如果它是一些样式设置命令,那么它会影响整个标题。

我们可以作个演示:

\documentclass{book}
\usepackage[a5paper,margin=1in]{geometry}
\usepackage{titlesec}
\usepackage{kantlipsum}
\begin{document}
\titleformat{\section}{\vspace{2cm}}{\S\,\thesection.}{0.5em}{}
% \vspace{2cm}在\section前面添加一段2cm的垂直距离
\titleformat{\subsection}{\bfseries}{\S\,\thesection.}{.5em}{}
% \bfseries将后面的\subsection设置成加粗字形
\chapter{A chapter}
This is a chapter
\section{A section}
\subsection{A subsection}
\kant[1]
\end{document}

编译:

在这里插入图片描述

  • label参数是一个必选参数,我们在其中重定义标头。还记得我们在前面(第005篇)说,如果我们想要"第X章"这标的章节标头,就需要重定义标头的结构。在这里我们就可以实现它。我们可以将标头定义成我们想要的任意样式。当然,我们将这个选项空着,什么都不写,也是可以的编译成功的,但是那样的后果就是定义的标题没有标头。我们演示一下:
\documentclass{book}
\usepackage[a5paper,margin=1in]{geometry}
\usepackage{titlesec}
\usepackage{kantlipsum}
\begin{document}
\titleformat{\chapter}[block]{\bfseries\centering}{}{0.5em}{}
% 我们将label参数空着
\chapter{A chapter}
\kant[1]
\end{document}

编译:

在这里插入图片描述

可以看到,章标题的标头没有了。总之,我们在label参数处填的任何内容都会被作为标头放在标题文本的前面。我们知道标头中包含了标题编号,那么我们格式化的时候最好将它们包含其中。我们用几个例子再演示一下(请认真阅读代码中的注释):

\documentclass{book}
\usepackage{ctex}
% 这里有中文,调用ctex包,并且编译时用xelatex
\usepackage[a5paper,margin=1in]{geometry}
\usepackage{titlesec}
\usepackage{kantlipsum}
\renewcommand{\thechapter}{\chinese{chapter}}
% 我们重定义chapter的标题编号,将计数器chapter格式化为中文输出(不理解的回顾一下第004篇)
\renewcommand{\thesection}{\arabic{chapter}.\arabic{section}}
% 重定义之后的章编号会影响到节编号(会在节编号中出现"一.1"这样的编号),为了避免这种影响,我们重定义\thesection
\begin{document}
\titleformat{\chapter}[block]{\bfseries\centering}{第\thechapter 章}{0.5em}{}
% 这里我们将标头重定义成"第X章"的格式
\titleformat{\section}{\bfseries}{\S\,\thesection.}{0.5em}{}
% \S是一个特殊的符号,我们将它放在section的编号的前面。
% "\,"生成一小段水平间距,比空格要小。为了美观,我们将它放在\S与编号中间
% 最后在编号后面加一个点.
\chapter{A chapter}
\section{A section}
\kant[1]
\end{document}

编译:

在这里插入图片描述

  • sep是一个必选参数,这里放置一个长度数值,表示标头与标题文本之间的距离。它根据shape参数来决定是水平距离还是垂直距离。我们前面的例子中,放置的长度都是0.5em,这里不再单独演示。
  • before-code是一个必选参数,这里放置的是在标题文本之前执行的代码。它的最后一个命令可以接收一个参数,它将使标题文本作为它的参数。也可以空着。我们演示一下:
\documentclass{book}
\usepackage{ctex}
\usepackage[a5paper,margin=1in]{geometry}
\usepackage{titlesec}
\usepackage{kantlipsum}
\renewcommand{\thechapter}{\chinese{chapter}}
\renewcommand{\thesection}{\arabic{chapter}.\arabic{section}}
\begin{document}
\titleformat{\chapter}[block]{\bfseries\centering}{第\thechapter 章}{0.5em}{\kaishu}
% \kaishu将中文设置成楷体
\titleformat{\section}{\bfseries}{\S\,\thesection.}{0.5em}{}
\titleformat{\subsection}{\bfseries}{\S\,\thesubsection.}{0.5em}{\tt}
% \tt设置成打印机字体
\chapter{这是一个章标题}
\section{这是一个节标题}
\subsection{A subsection}
\kant[1]
\end{document}

编译:

在这里插入图片描述

在新版的 LaTeX \LaTeX LATEX中,我们在加载titlesec包时可以指定explicit选项,然后在before-code参数的内部必须通过#1来指定标题文本。这实际上让我们可以更自由地设计标题文本的样式了,甚至可以用这个参数在标题文本后面放置内容。我们作个演示:

\documentclass{book}
\usepackage{ctex}
\usepackage[a5paper,margin=1in]{geometry}
\usepackage[explicit]{titlesec}
% 指定选项explicit
\usepackage{kantlipsum}
\renewcommand{\thechapter}{\chinese{chapter}}
\renewcommand{\thesection}{\arabic{chapter}.\arabic{section}}
\begin{document}
\titleformat{\chapter}[block]{\bfseries\centering}{第\thechapter 章}{0.5em}{{\kaishu#1}}
\titleformat{\section}{\bfseries}{\S\,\thesection.}{0.5em}{}
% before-code空着,标题文本就不会显示
\titleformat{\subsection}{\bfseries}{\S\,\thesubsection.}{0.5em}{{\tt#1}\hspace{0.2cm}\LaTeX}
% 将标题文本设置为打印机字体,并且在后面放置一上0.2cm的水平间距以及\LaTeX
\chapter{这是一个章标题}
\section{这是一个节标题}
\subsection{A subsection}
\kant[1]
\end{document}

编译:

在这里插入图片描述

  • after-code是一个可选参数,它是在标题文本后面执行的代码。对不同的shape,它的位置也不同。对runin(内嵌标题),after-code会放在标题的正后方,对其它的情形(阵列标题),after-code会放在标题的下方。我们作个演示:
\documentclass{book}
\usepackage{ctex}
\usepackage[a5paper,margin=1in]{geometry}
\usepackage{titlesec}
\usepackage{kantlipsum}
\renewcommand{\thechapter}{\chinese{chapter}}
\renewcommand{\thesection}{\arabic{chapter}.\arabic{section}}
\begin{document}
\titleformat{\chapter}[block]{\bfseries\centering}{第\thechapter 章}{0.5em}{\kaishu}[\LaTeX]
\titleformat{\section}[hang]{\bfseries}{\S\,\thesection.}{0.5em}{}[\LaTeX]
\titleformat{\subsection}[runin]{\bfseries}{\S\,\thesubsection.}{0.5em}{\tt}[\LaTeX]
\chapter{这是一个章标题}
\section{这是一个节标题}
\subsection{A subsection}
\kant[1]
\end{document}

编译:

在这里插入图片描述

我们在每个标题的after-code处放置了一个 LaTeX \LaTeX LATEX​。

现在思考一个问题,如果我们在调用titlesec包时指定了选项explicit,并且在一个shaperunin的标题的before-code中的#1后面放置了一些内容,又在after-code处放置了一些内容,哪些内容会在前面,哪些会在后面?我们测试一下:

\documentclass{book}
\usepackage{ctex}
\usepackage[a5paper,margin=1in]{geometry}
\usepackage[explicit]{titlesec}
\usepackage{kantlipsum}
\renewcommand{\thechapter}{\chinese{chapter}}
\renewcommand{\thesection}{\arabic{chapter}.\arabic{section}}
\begin{document}
\titleformat{\chapter}[block]{\bfseries\centering}{第\thechapter 章}{0.5em}{\kaishu#1}
\titleformat{\section}[runin]{\bfseries}{\S\,\thesection.}{0.5em}{#1\hspace{3mm}before-code}[\hspace{3mm}after-code]
\chapter{这是一个章标题}
\section{这是一个节标题}
\kant[1]
\end{document}

编译:

在这里插入图片描述

可以看出,这种情况下before-code仍旧是在after-code前面的。

如果我们使用了划分命令的星号形式,那么在label以及sep中设置的参数都不会生效,因为星号形式不产生标头。

默认情况下, LaTeX \LaTeX LATEX的划分命令是不缩进的,要么居中,要么居左。如果我们想要一个缩进的内嵌(runin)标题,就在标头前面(label参数的最前面)加一个\indent。还有一种方法就是使用\titlespacing命令指定缩进。\titlespacing命令是我们接下来要讲的命令。

\titlespacing有一个星号形式的命令\titlespacing*,它和不带星号的命令不同的地方是,它会抑制标题后第一段的首行缩进,除非第一段和标题是结合在一起的,例如形状是runin或者drop的标题。\titlespacing的命令形式如下:

\titlespacing{cmd}{left-sep}{before-sep}{after-sep}[right-sep]
  • cmd命令仍然是章节划分命令,如\section等。
  • left-sep,输入一个长度数值(后面的几个参数也一样),它根据\titleformatshape参数的不同,而代表不同位置的距离。对于blockdisplayhang、以及frame来说,它指定标题处左边距的增加的距离,对于margindrop来说,它指定了标题的宽度,对于wrap来说,它指定了标题的最大宽度,而对于runin来说,它指定了标题左侧的缩进距离(如果是负数,标题会反向缩进,进入左边距里)。我们作几个演示:
\documentclass{article}
\usepackage[a5paper,margin=1in]{geometry}
\usepackage{titlesec}
\usepackage{kantlipsum}
\begin{document}
\titleformat{\section}[hang]{\bfseries}{\S\,\thesection.}{0.5em}{}
\titlespacing{\section}{2cm}{*4}{*2.3}[1pc]
% 我们只关注left-sep参数,后面的先不用去管
\section{This is a section}
\kant[1]
\end{document}

编译:

在这里插入图片描述

如果我们\sectionshape换成display,编译结果则是这样的:

在这里插入图片描述

如果换成frame, 则是:

在这里插入图片描述

对于以上的shape(包括block),left-sep参数中的2cm,意思就是标题的左边距在原来的基础上再增加2cm。如果换成leftmargin,则是:

在这里插入图片描述

注意,在以上的shape中,如果我们将\titlespacing换成\titlespacing*,那么第一段的首行就不会缩进。这个请自行演示。

如果换成drop, 则是:

在这里插入图片描述

margindropshape中,2cm是标题的宽度。

如果换成runin,2cm则是标题的缩进距离:

在这里插入图片描述

注意,缩进和第一种情形不同,在runin情形下,如果标题很长的话,第二行是不会与第一行对齐的,我们将标题换成长标题,并将hangrunin作一下对比:

\documentclass{article}
\usepackage[a5paper,margin=1in]{geometry}
\usepackage{titlesec}
\usepackage{kantlipsum}
\begin{document}
\titleformat{\section}[hang]{\bfseries}{\S\,\thesection.}{0.5em}{}
\titlespacing{\section}{4cm}{*4}{*2.3}[1pc]
\titleformat{\subsection}[runin]{\bfseries}{\S\,\thesubsection.}{0.5em}{}
\titlespacing{\subsection}{4cm}{*4}{*2.3}[1pc]
\section{This is a very long title of a section}
\subsection{This is a very long title of a subsection}
\kant[1]
\end{document}

编译:

在这里插入图片描述

缩进距离可以是负数:

\documentclass{article}
\usepackage[a5paper,margin=1in]{geometry}
\usepackage{titlesec}
\usepackage{kantlipsum}
\begin{document}
\titleformat{\section}[runin]{\bfseries}{\S\,\thesection.}{0.5em}{}
\titlespacing{\section}{-2cm}{*4}{*2.3}[1pc]
\section{This is a section}
\kant[1]
\end{document}

编译:

在这里插入图片描述

  • before-sep输入一个长度数值,它表示在标题上方增加的垂直距离。
  • after-sep输入一个长度数值,它表示标题与后面的段落之间的距离。根据shape的不同,它可以是垂直距离,也可以是水平距离。我们作个演示:
\documentclass{article}
\usepackage[a5paper,margin=1in]{geometry}
\usepackage{titlesec}
\usepackage{kantlipsum}
\begin{document}
\titleformat{\section}[hang]{\bfseries}{\S\,\thesection.}{0.5em}{}
\titlespacing{\section}{0cm}{0cm}{2cm}[1pc]
% 我们设置\section距离下一段的距离是2cm。由于shape是hang,所以是垂直距离
\titleformat{\subsection}[runin]{\bfseries}{\S\,\thesubsection.}{0.5em}{}
\titlespacing{\subsection}{0cm}{1cm}{2cm}[1pc]
% 设置\subsection上方的距离增加1cm,距离下一段的距离是2cm,由于shape是runin,所以是水平距离。
\section{This is a section}
\kant[1]
\subsection{This is a subsection}
\kant[2]
\end{document}

编译:

在这里插入图片描述

  • right-sep是一个可选参数,它表示右边距的增加距离,它只支持blockdisplayhangframe。除非另外设置,标题默认是靠左对齐的,只有标题足够长,碰到右边的边界时,它的作用才能凸显出来:
\documentclass{article}
% \usepackage{ctex}
\usepackage[a5paper,margin=1in]{geometry}
\usepackage{titlesec}
\usepackage{kantlipsum}
\begin{document}
\titleformat{\section}[hang]{\bfseries}{\S\,\thesection.}{0.5em}{}
\titlespacing{\section}{3cm}{0cm}{*1}[2cm]
% 我们用一个长标题,并且左边距增加3cm,右边距增加2cm
\section{This is a very long title of a section}
\kant[2]
\end{document}

编译:

在这里插入图片描述

正常情况下,对一个嵌入(runin)标题,after-sep是标题与段落之间的水平距离。默认情况下,它比一个句子中两个单词的正常间距要大得多,因为它的用途就是将标题和段落分开。但在某些情况下,我们希望标题和段首是一个完整的句子,那么这个时候我们就需要一个正常的单词间距放在after-sep处,我们可以使用变量\wordsep来表示当前字体的单词间距:

\documentclass{article}
\usepackage[a5paper,margin=1in]{geometry}
\usepackage{titlesec}
\usepackage{kantlipsum}
\begin{document}
\titleformat {\section}[runin]{\normalfont\scshape}{}{0pt}{}
\titlespacing{\section}{\parindent}{\medskipamount}{\wordsep}
\section{The man} started to run away from the truck.
He saw that he was followed by the \ldots
\end{document}

编译:

在这里插入图片描述
关注【年轻人 你渴望力量么】

  • 16
    点赞
  • 16
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值