标题中的部分名称

标题中的部分名称

我正在写论文报告,我想将章节名称保留在页面的页眉中。现在,页码位于页脚的中央,我想以同样的方式保留它。我尝试使用 fancyhdr 来保留章节名称,但问题是,它还会显示内容的页眉和图表列表,这些都在 \frontmatter 中。任何建议都会有所帮助。

%\documentclass[12pt,a4paper]{article}
\documentclass[12pt,a4paper]{article}   
\setcounter{tocdepth}{3}
\setcounter{secnumdepth}{3}

%function definition for starting intro from first section.
\def\frontmatter{%
    \pagenumbering{roman}
    \setcounter{page}{1}
    \renewcommand{\thesection}{\Roman{section}}
}%

\def\mainmatter{%
    \pagenumbering{arabic}
    \setcounter{page}{1}
    \setcounter{section}{0}
    \renewcommand{\thesection}{\arabic{section}}
}%
\usepackage{fontspec}
%\setmainfont{Times New Roman}

\usepackage{makecell}
\usepackage{graphicx}

预期看起来是这样的: 在此处输入图片描述

答案1

您可以使用fancyhdr和定义自己的页面样式,并在和\pagestyle{...}的定义末尾使用。\mainmatter\frontmatter
页面样式的设置

% Page Layout
\setlength{\headheight}{15pt} % otherwise fancyhdr gives warning
\fancyhf{}

\fancypagestyle{frontmatter}{
    \fancyhf{} % clearing headers and footers
    \fancyfoot[C]{\thepage} % page number centered in the footer
    \renewcommand{\headrulewidth}{0pt} % Create an invisible header ruler line
}

\fancypagestyle{mainmatter}{
    \fancyhf{}
    \fancyhead[C]{\leftmark} % \leftmark gives current section with its number you may want to use \nouppercase
    \fancyfoot[C]{\thepage}
    \renewcommand{\headrulewidth}{0.3pt} % Create an header ruler line
}

答案2

根据您的要求,无需软件包即可实现fancyhdr。以下是示例代码:

\documentclass[12pt,a4paper]{article}   
\setcounter{tocdepth}{3}
\setcounter{secnumdepth}{3}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% pagestyle `headings` setting %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\makeatletter
\def\ps@headings{%
    \def\@oddfoot{\hfil\thepage\hfil}
    \def\@oddhead{\hfil\slshape\rightmark\hfil}%
    \def\@evenhead{\hfil\slshape\rightmark\hfil}%
    \let\@mkboth\markboth
    \def\sectionmark##1{%
      \markright {%
        \ifnum \c@secnumdepth >\m@ne
          \thesection\quad
        \fi
        ##1}}}
\makeatother

%%%%%%%%%%%%%%%%%%
% other packages %
%%%%%%%%%%%%%%%%%%
\usepackage{fontspec}
%\setmainfont{Times New Roman}
\usepackage{makecell}
\usepackage{graphicx}
\usepackage{lipsum}
\usepackage{mwe}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{document}
%%%%%%%%%%%%%%
% title page %
%%%%%%%%%%%%%%
\thispagestyle{empty}
\null\vfil
\begin{center}
\textbf{My Title}
\vskip 3em
\textbf{My Name}
\end{center}
\vfil\null
\clearpage

%%%%%%%%%%%%%%%
% frontmatter %
%%%%%%%%%%%%%%%
\pagenumbering{roman}
\tableofcontents
\clearpage

\listoffigures
\clearpage

\listoftables
\clearpage

%%%%%%%%%%%%%%
% mainmatter %
%%%%%%%%%%%%%%
\pagenumbering{arabic}
\pagestyle{headings}
\section{Introduction}
\lipsum[1]
\clearpage

\section{Analysis}
\lipsum[2]
\begin{figure}[ht]
    \centering
    \includegraphics{example-image-a}
    \caption{figure 1}
    \label{fig:my_label}
\end{figure}
\clearpage

\section{Results}
\begin{table}[ht]
    \centering
    \begin{tabular}{c|c}
         1&2  \\
         3&4 
    \end{tabular}
    \caption{table 1}
    \label{tab:my_label}
\end{table}
\lipsum[3]

\end{document}

相关内容