ヘッダーのセクション名

ヘッダーのセクション名

私は論文レポートを書いていますが、ページのヘッダーにセクション名を入れたいと思っています。現在、ページ番号はフッターの中央にありますが、これを同じ方法で維持したいと考えています。セクション名を維持するために 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}

関連情報