標題中的部分名稱

標題中的部分名稱

我正在撰寫論文報告,我想將章節名稱保留在頁眉中。現在,頁碼位於頁腳的中心,我想以同樣的方式保留它。我嘗試使用 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}

相關內容