使用 titlesec 如果章节和节之间没有文本,如何防止它们之间出现空格?

使用 titlesec 如果章节和节之间没有文本,如何防止它们之间出现空格?

我被一个非常严格的要求所困扰,即保持章节/节/小节/段落标题之间的间距相同。章节标题后,我需要三倍空格。节标题后,我需要双倍空格。我可以做到这一点,但是,如果章节标题和第一节标题之间没有其他文本,则空格将相加(三倍空格 + 双倍空格)。有没有办法让 if 语句确认如果有文本,则仅使用三倍空格,而如果显示文本,则使用双倍空格?

\titlespacing{\chapter}{0in}{\UOneInChapterSpace}{\UHeadSpace}[0in] 
\titlespacing{\section}{0in}{0in}{\UDoubleSpace}[0in]
\titlespacing{\subsection}{0in}{0in}{\UDoubleSpace}[0in]
\titlespacing{\subsubsection}{0in}{0in}{\UDoubleSpace}[0in]
\titlespacing{\paragraph}{1em}{1ex}{1ex}[1em]

\titleformat{\chapter}
    [display]
    {\color{URED}\centering\normalfont\Large\bfseries\doublespacing}
    {\MakeUppercase{\chaptertitlename} \Large\thechapter}
    {0in}
    {\MakeUppercase}
    []

\titleformat{\section}
    [hang]
    {\centering\singlespacing\normalfont\Large\bfseries}
    {\thesection}
    {1ex}
    {}
    []

\titleformat{\subsection}
    [hang]
    {\centering\singlespacing\normalfont\large\bfseries}
    {\thesubsection}
    {1ex}
    {}
    []

\titleformat{\subsubsection}
    [hang]
    {\centering\singlespacing\normalfont\normalsize\bfseries}
    {\thesubsubsection}
    {1ex}
    {}
    []

\titleformat{\paragraph}
    [runin]
    {\centering\singlespacing\normalfont\normalsize\bfseries}
    {\theparagraph}
    {1ex}
    {}
    []

\titleformat{\subparagraph}
    [runin]
    {\centering\singlespacing\normalfont\normalsize\bfseries}
    {\thesubparagraph}
    {1ex}
    {}
    []

我发布了一个包含整个 cls 文件(关联)。我只是不确定该怎么做。

谢谢你!

答案1

您可以将以下设置纳入您的课程。网格设置为\baselineskip双倍行距。如果没有文本,则标题和部分后不会出现额外空间。

%%***************************************************** titlespacing starts
\newlength{\Beforesec}
\setlength{\Beforesec}{2ex}
\newlength{\Aftersec}
\setlength{\Aftersec}{1ex}

\setlength{\UOneInChapterSpace}{-45pt + 1in} 
\setlength{\UHeadSpace}{3\baselineskip}
\setlength{\UDoubleSpace}{2\baselineskip}   

\titlespacing{\chapter}{0in}{\UOneInChapterSpace}{\UHeadSpace+\Beforesec}[0in] 
\titlespacing{\section}{0in}{\Beforesec}{\UDoubleSpace+\Aftersec}[0in]
\titlespacing{\subsection}{0in}{0in}{0.5\UDoubleSpace}[0in]
\titlespacing{\subsubsection}{0in}{0in}{0.5\UDoubleSpace}[0in]
\titlespacing{\paragraph}{1em}{1ex}{1ex}[1em]
%%***************************************************** titlespacing ends

标题后的部分中间无文字

瓦

标题后的文字

是

测试此代码

% !TeX TS-program = pdflatex

\documentclass{MastersDoctoralThesis}

\usepackage{eso-pic}
\AddToShipoutPictureBG{% GRID
    \begin{tikzpicture}[remember picture, overlay,
        help lines/.append style={line width=0.05pt,
            color=white},
        major divisions/.style={help lines,line width=0.5pt,
            color=blue!40} ]
        \draw[help lines] (current page.south west) grid[step=0.5pt]
        (current page.north east);
        \draw[major divisions] (current page.south west) grid[step=22pt] % baselineskip = 22pt at double space
        (current page.north east);
    \end{tikzpicture}%
}   

%%***************************************************** titlespacing starts
\newlength{\Beforesec}
\setlength{\Beforesec}{2ex}
\newlength{\Aftersec}
\setlength{\Aftersec}{1ex}

\setlength{\UOneInChapterSpace}{-45pt + 1in} 
\setlength{\UHeadSpace}{3\baselineskip}
\setlength{\UDoubleSpace}{2\baselineskip}   

\titlespacing{\chapter}{0in}{\UOneInChapterSpace}{\UHeadSpace+\Beforesec}[0in] 
\titlespacing{\section}{0in}{\Beforesec}{\UDoubleSpace+\Aftersec}[0in]
\titlespacing{\subsection}{0in}{0in}{0.5\UDoubleSpace}[0in]
\titlespacing{\subsubsection}{0in}{0in}{0.5\UDoubleSpace}[0in]
\titlespacing{\paragraph}{1em}{1ex}{1ex}[1em]
%%***************************************************** titlespacing ends

\begin{document}
\mainMatter %% sets\doublespacing 

\chapter[\uppercase{Multiline title}]{\uppercase{Multiline \protect \\  title}} 
1   \lipsum[1]
\section{Section1}
2   \lipsum[2]
\section{Section2}
\section{Section3}
\section{Section4}
3   \lipsum[3]
\subsection{Subsection1}
4   \lipsum[4]  
\subsection{Subsection2}    
\subsection{Subsection3}    
\subsection{Subsection4}
5   \lipsum[5]  
\paragraph{Paragraph1}
6   \lipsum[6]  
\paragraph{Paragraph2}
\paragraph{Paragraph3}
\paragraph{Paragraph4}
7   \lipsum[7]
\end{document

相关内容