现在的情况

现在的情况

现在的情况

我的文档 ( report) 由章节、节和小节组成。现在我想在页脚左侧显示节名称和小节名称。但是,如果我使用,\rightmark我只会得到节名称,而不会得到小节名称。

一个最小的例子

我试图将问题简化为这个最小的例子,它基本上是我实际文档中的代码:

\documentclass[a4paper,12pt]{report}

% <header_footer>
    \usepackage{fancyhdr}
    \pagestyle{fancy}
    \fancyhf{}
    \fancyhead[L]{\rightmark}
% </header_footer>

% ...
% a lot of other stuff here
% ...

% <toc>
    \setcounter{secnumdepth}{0}

    \newcommand\invisiblesection[1]{%
        \refstepcounter{section}%
        \addcontentsline{toc}{section}{#1}%
        \sectionmark{#1}}

    \newcommand\invisiblesubsection[1]{%
        \refstepcounter{subsection}%
        \addcontentsline{toc}{subsection}{#1}%
        \subsectionmark{#1}}
% </toc>

\begin{document}
    \tableofcontents
    \newpage

    \chapter{a chapter}
    \newpage

    \invisiblesection{section}
    \invisiblesubsection{subsection}
    content to ensure the page is there
    \newpage

    \invisiblesection{another section}
    \invisiblesubsection{subsection 1}
    content to ensure the page is there 
    % the header should now display "another section - subsection 1"
    \newpage

    \invisiblesubsection{subsection 2}
    content to ensure the page is there 
    % the header should now display "another section - subsection 2"
    \newpage

    \invisiblesubsection{subsection 3}
    content to ensure the page is there 
    % the header should now display "another section - subsection 3"
\end{document}

外观

图像显示它的样子

它应该是什么样子

显示其应有外观的图像

我迄今为止尝试过

  • 覆盖两个标记 - 不起作用,因为\leftmark被定义为章节名称
  • 获取当前部分名称nameref- 无法工作,因为它正在获取章节名称
  • \def每次\invisiblesection创建时都设置自定义变量 - 也不起作用

问题

我想只有一个非常简单的标志,我忘了,但谷歌没有告诉我。有人有解决方案可以在页眉/页脚中同时显示部分和子部分的名称吗?

答案1

\subsectionmark命令被定义为不执行任何操作,因此如果您希望它发出标记,则需要更改其定义。

同样,你必须改变\sectionmark,以便没有发出标记。

\documentclass[a4paper,12pt]{report}

\usepackage{fancyhdr}
\pagestyle{fancy}
\fancyhf{}
\fancyhead[L]{\rightmark}

\setlength{\headheight}{14.5pt}
\setcounter{secnumdepth}{0}

\newcommand\invisiblesection[1]{%
  \refstepcounter{section}%
  \addcontentsline{toc}{section}{#1}%
  \sectionmark{#1}%
}

\newcommand\invisiblesubsection[1]{%
  \refstepcounter{subsection}%
  \addcontentsline{toc}{subsection}{#1}%
  \subsectionmark{#1}%
}

\renewcommand{\sectionmark}[1]{%
  \gdef\currsection{#1}%
}
\renewcommand{\subsectionmark}[1]{%
  \markright{\currsection\ \textbullet\ #1}%
}

\begin{document}

\tableofcontents
\newpage

\invisiblesection{section}
\invisiblesubsection{subsection}
content to ensure the page is there
\newpage

\invisiblesection{another section}
\invisiblesubsection{subsection 1}
content to ensure the page is there 
% the header should now display "another section - subsection 1"
\newpage

\invisiblesubsection{subsection 2}
content to ensure the page is there 
% the header should now display "another section - subsection 2"
\newpage

\invisiblesubsection{subsection 3}
content to ensure the page is there 
% the header should now display "another section - subsection 3"

\end{document}

这是第 4 页的顶部

在此处输入图片描述

相关内容