未编号的章节和章节标记

未编号的章节和章节标记

在我的文档中,我有编号和未编号的部分。未编号的部分如下所示:

\section*{Introduction}

我使用 fancyhdr 并添加像这样的章节和小节标记。

\pagestyle{fancy}
\fancyhf{} % clear all header and footers
\renewcommand{\sectionmark}[1]{\markboth{\thesection\ #1}{}}
\renewcommand{\subsectionmark}[1]{\markright{\thesubsection\ #1}}
\fancyhead[L]{\rule[-0.25in]{0pt}{0.25in}\parbox{0.9\textwidth}{%
  \hdrfont\textbf{\leftmark\\\rightmark}}}
\fancyhead[R]{\hdrfont\thepage\ /~\pageref{LastPage}}
\fancyfoot[L]{\rule[-0.25in]{0pt}{0.25in}\parbox{0.9\textwidth}{%
  \hdrfont\textbf{\leftmark\\\rightmark}}}
\fancyfoot[R]{\hdrfont\thepage\ /~\pageref{LastPage}}
\renewcommand{\headrulewidth}{0.4pt}
\renewcommand{\footrulewidth}{0.4pt}

因此,未编号的部分在标题中会被忽略。

我尝试在未编号的部分中使用此命令

\sectionmark{Introduction}

现在,我在标题中为这些部分设定了 0 个索引。

如何修复此问题?有没有解决方案可以创建未编号部分,并将其视为编号部分(没有 sectionmark、addcontentsline 等)?

平均能量损失

\documentclass[12pt]{article}

\usepackage{lipsum}
\usepackage{fancyhdr}

% -----------------------------------------------------------------------------
\pagestyle{fancy}
\fancyhf{} % clear all header and footers
\renewcommand{\sectionmark}[1]{\markboth{\thesection\ #1}{}}
\renewcommand{\subsectionmark}[1]{\markright{\thesubsection\ #1}}
\fancyhead[L]{\rule[-0.25in]{0pt}{0.25in}\parbox{0.9\textwidth}{%
  \textbf{\leftmark\\\rightmark}}}
\fancyhead[R]{\thepage}
\fancyfoot[L]{\rule[-0.25in]{0pt}{0.25in}\parbox{0.9\textwidth}{%
  \textbf{\leftmark\\\rightmark}}}
\fancyfoot[R]{ \thepage}
\renewcommand{\headrulewidth}{0.4pt}
\renewcommand{\footrulewidth}{0.4pt}

\begin{document}
\tableofcontents
\section{Section1}
\lipsum[4-8]
\newpage
\section*{Section2}
\lipsum[4-8]
\newpage
\section{Section3}
\lipsum[4-8]
\newpage
\section*{Section4}
\lipsum[4-8]
\end{document}

答案1

\markboth执行时发出适当的命令\section*;最简单的方法,无需深入研究的定义\@startsection(可能不适用于)titlesec,是\section在的帮助下重新定义xparse

\documentclass[12pt]{article}

\usepackage[compact]{titlesec}
\usepackage{fancyhdr}
\usepackage{lipsum}

\usepackage{xparse}
\let\CLASSsection\section

\RenewDocumentCommand{\section}{som}{%
  \IfBooleanTF{#1}
   {% there's a *
    \CLASSsection*{#3}\markboth{#3}{}%
   }
   {% no *
    \IfNoValueTF{#2}
     {% no opt arg
      \CLASSsection{#3}%
     }
     {% opt arg
      \CLASSsection[#2]{#3}%
     }%
   }%
}

\makeatletter
% fix \tableofcontents
\renewcommand{\tableofcontents}{%
  \section*{\contentsname}%
  \@starttoc{toc}%
}
\makeatother


% -----------------------------------------------------------------------------
\pagestyle{fancy}
\fancyhf{} % clear all header and footers
\renewcommand{\sectionmark}[1]{\markboth{\thesection\ #1}{}}
\renewcommand{\subsectionmark}[1]{\markright{\thesubsection\ #1}}

\fancyhead[L]{%
  \bfseries\begin{tabular}{@{}l@{}}\mbox{}\leftmark\\ \mbox{}\rightmark\end{tabular}%
}
\fancyhead[R]{\thepage}
\fancyfoot[L]{%
  \bfseries\begin{tabular}{@{}l@{}}\mbox{}\leftmark\\ \mbox{}\rightmark\end{tabular}%
}
\fancyfoot[R]{\thepage}
\renewcommand{\headrulewidth}{0.4pt}
\renewcommand{\footrulewidth}{0.4pt}

\setlength{\headheight}{30pt}

\begin{document}
\tableofcontents
\clearpage
\section{Section1}
\lipsum[4-8]
\newpage
\section*{Section2}
\lipsum[4-8]
\newpage
\section{Section3}
\lipsum[4-8]
\newpage
\section*{Section4}
\lipsum[4-8]
\end{document}

我还重新定义了如何设置页眉和页脚;当没有正确的标记可排版时, atabular肯定比拥有大量消息要好。还需要重新定义,因为它通常会发出( 的别名)。Underfull \hbox\tableofcontents\@mkboth\markboth

不要忘记遵循fancyhdr关于增加的建议\headheight;使用此设置,它只需要略少于 30pt。

答案2

这用于\pretocmd修补涉及排版星号部分的合适的内部 latex2e 宏。

\documentclass{article}
\usepackage{hyperref}% checking compatibility
\usepackage{fancyhdr}
\pagestyle{fancy}
\fancyhf{} % clear all header and footers

\let\hdrfont\relax % don't know what this font is supposed to be
\AtEndDocument{\label{LastPage}} % very deficient work-around for LastPage which comes from some package.

\renewcommand{\sectionmark}[1]{\markboth{\thesection\ #1}{}}
\renewcommand{\subsectionmark}[1]{\markright{\thesubsection\ #1}}
\fancyhead[L]{\rule[-0.25in]{0pt}{0.25in}\parbox{0.9\textwidth}{%
  \hdrfont\textbf{\leftmark\\\rightmark}}}
\fancyhead[R]{\hdrfont\thepage\ /~\pageref{LastPage}}
\fancyfoot[L]{\rule[-0.25in]{0pt}{0.25in}\parbox{0.9\textwidth}{%
  \hdrfont\textbf{\leftmark\\\rightmark}}}
\fancyfoot[R]{\hdrfont\thepage\ /~\pageref{LastPage}}
\renewcommand{\headrulewidth}{0.4pt}
\renewcommand{\footrulewidth}{0.4pt}

\providecommand\phantomsection{}

\usepackage{etoolbox}
\makeatletter
\def\@@@SECTION{section}
\pretocmd{\@startsection}{\def\@@@SECTIONING{#1}}{}{}
\pretocmd{\@ssect}{%
   \expandafter\in@\expandafter{\@@@SECTIONING}{section,subsection}%
   \ifin@
     \phantomsection
     \addcontentsline{toc}{\@@@SECTIONING}{#5}%
     \ifx\@@@SECTIONING\@@@SECTION
          \markboth{#5}{}%
     \else\markright{#5}%
     \fi
   \fi}{}{}
\makeatother 
\begin{document}

\tableofcontents
\section*{Introduction}

one\newpage
\section*{Unnumbered}

two\newpage
\section{Numbered}

three\newpage

\section{Again Numbered}

four
\end{document}

相关内容