确保章节标题出现在页面顶部且间隙最小

确保章节标题出现在页面顶部且间隙最小

我的主管让我插入以下内容:

\titleformat{\chapter}[display]%This puts chapter titles right at the top and changes line spacing
{\normalfont\huge\bfseries}{\chaptertitlename\ \thechapter}{10pt}{\Huge}   
\titlespacing*{\chapter}{0pt}{-70pt}{15pt}
\renewcommand{\baselinestretch}{1.5}

在我的文档顶部,基本上使我的章节标题出现在新章节的每一页的顶部。

您可以运行它来查看它的作用:

\documentclass[12pt]{report}% Not sure what this is for
\setcounter{secnumdepth}{4}% For quotes in chapters
\usepackage{epigraph}% This is for named paragraphs
\usepackage[explicit]{titlesec}% This is for aligned equations
\usepackage{listings}%This is for code listings
\usepackage{color}

\definecolor{dkgreen}{rgb}{0,0.6,0}
\definecolor{gray}{rgb}{0.5,0.5,0.5}
\definecolor{mauve}{rgb}{0.58,0,0.82}

\lstset{frame=tb,
  language=Java,
  aboveskip=3mm,
  belowskip=3mm,
  showstringspaces=false,
  columns=flexible,
  basicstyle={\small\ttfamily},
  numbers=none,
  numberstyle=\tiny\color{gray},
  keywordstyle=\color{blue},
  commentstyle=\color{dkgreen},
  stringstyle=\color{mauve},
  breaklines=true,
  breakatwhitespace=true,
  tabsize=3
}
\usepackage{algorithm}% http://ctan.org/pkg/algorithms
\usepackage{algpseudocode}%http://ctan.org/pkg/algorithmicx
% \usepackage[linesnumbered,ruled]{algorithm2e}
\algrenewcommand\textproc{}% Used to be \textsc
\renewcommand{\algorithmicrequire}{\textbf{Input:}}
\renewcommand{\algorithmicensure}{\textbf{Output:}}
\usepackage{calc} % for \widthof
\algrenewcommand\algorithmicensure{%
  \makebox[\widthof{\textbf{Require:}}][l]{\textbf{Ensure:}}} 
\titleformat{\chapter}[display]%This puts chapter titles right at the top and changes line spacing
{\normalfont\huge\bfseries}{\chaptertitlename\ \thechapter}{10pt}{\Huge}   
\titlespacing*{\chapter}{0pt}{-70pt}{15pt}
\renewcommand{\baselinestretch}{1.5}

\begin{document}

\chapter{Implementation of Approach}\label{implementation}

lorem ipsum

\end{document}

可以看到结果是:

在此处输入图片描述

这意味着它摆脱了我的章节标题Implementation of Approach,并且仍然在章节标题和下一行文本之间保留了巨大的间隙(lorem ipsum)。

我在这里遗漏了什么?我该怎么做才能解决上述两个问题?

答案1

您已指定明确的titlesec 包的选项。这意味着您必须\chapter通过#1代码前的参数\titleformat。如果我对您的宏调用进行以下更改,它将解决您的两个问题:

\titleformat{\chapter}[display]%This puts chapter titles right at the top and changes line spacing
{\normalfont\huge\bfseries}{\chaptertitlename\ \thechapter}{10pt}{\Huge #1}

完整示例:

\documentclass[12pt]{report}% Not sure what this is for
\setcounter{secnumdepth}{4}% For quotes in chapters
\usepackage{epigraph}% This is for named paragraphs
\usepackage[explicit]{titlesec}% This is for aligned equations
\usepackage{listings}%This is for code listings
\usepackage{color}

\definecolor{dkgreen}{rgb}{0,0.6,0}
\definecolor{gray}{rgb}{0.5,0.5,0.5}
\definecolor{mauve}{rgb}{0.58,0,0.82}

\lstset{frame=tb,
  language=Java,
  aboveskip=3mm,
  belowskip=3mm,
  showstringspaces=false,
  columns=flexible,
  basicstyle={\small\ttfamily},
  numbers=none,
  numberstyle=\tiny\color{gray},
  keywordstyle=\color{blue},
  commentstyle=\color{dkgreen},
  stringstyle=\color{mauve},
  breaklines=true,
  breakatwhitespace=true,
  tabsize=3
}
\usepackage{algorithm}% http://ctan.org/pkg/algorithms
\usepackage{algpseudocode}%http://ctan.org/pkg/algorithmicx
% \usepackage[linesnumbered,ruled]{algorithm2e}
\algrenewcommand\textproc{}% Used to be \textsc
\renewcommand{\algorithmicrequire}{\textbf{Input:}}
\renewcommand{\algorithmicensure}{\textbf{Output:}}
\usepackage{calc} % for \widthof
\algrenewcommand\algorithmicensure{%
  \makebox[\widthof{\textbf{Require:}}][l]{\textbf{Ensure:}}}
\titleformat{\chapter}[display]%This puts chapter titles right at the top and changes line spacing
{\normalfont\huge\bfseries}{\chaptertitlename\ \thechapter}{10pt}{\Huge #1}
\titlespacing*{\chapter}{0pt}{-70pt}{15pt}
\renewcommand{\baselinestretch}{1.5}

\begin{document}

\chapter{Implementation of Approach}\label{implementation}

lorem ipsum

\end{document}

相关内容