为什么 LaTeX 使用了错误的标题?

为什么 LaTeX 使用了错误的标题?

根据我的问题“检查号码是否在列表中“我还有另一个问题:

我使用了 Steven 的答案来获取两个列表:highlightedProjects 和 normalProjects。项目是位于项目文件夹中的 tex 文件,其名称类似于“project_”+ 从 01 开始的数字。现在,我首先尝试从 highlightedProjects 获取所有项目,并为它们指定标题“Highlighted Project”。在所有突出显示的项目之后,我希望从 normalProjects 获取所有其他带有标题“Other Projects”的项目。但是下面的示例代码的结果带有错误的标题。顺序是正确的,第一个项目是项目 03,但标题为“Other Projects”。除最后一个项目外,其他项目具有相同的标题。它有默认标题“title”??我做错了什么?

这是我的代码:

\documentclass[10pt]{article}
\usepackage[export]{adjustbox}
\usepackage[a4paper, left=2cm,top=0.8cm,right=2cm,bottom=3.3cm,headheight=61pt, footskip=61pt, includehead]{geometry}
\usepackage{wrapfig}
\usepackage[default]{opensans}
\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}
\usepackage[english,ngerman]{babel}
\usepackage{fancyhdr}
\usepackage{tikz}
\usepackage{xifthen}
\usepackage{listofitems}

\fancypagestyle{plain}{%
  \renewcommand{\headrulewidth}{0pt}%
  \fancyhf{}%
  \lhead{title}
}
\pagestyle{plain} % so LaTeX updates the definition
\def\myHighlights{03}   
\def\myList{01, 02, 03, 04, 05, 06, 07, 08, 09, 10, 11, 12, 13, 14, 15,
  16, 17, 18, 19, 20}


\begin{document}
%invert sort (i need it because of the history of the projects)
\let\mylist\empty
  \foreach\x in \myList {
    \ifx\mylist\empty
      \xdef\mylist{\x}%
      \else
      \xdef\mylist{\x,\mylist}%
    \fi
  }

\newcommand\normalProjectList{}
\newcommand\highlightedProjectList{}
 
  %if I don't have highlights:
\ifx\myHighlights\empty
  \lhead{Project records}
  \foreach\x in \mylist {%  
    \edef\projectNumber{\x}%
    \edef\FileName{projects/project_\x}%     
    \IfFileExists{\FileName}{%  
       \newpage%
     \input{\FileName}%       
    }
  }
\else
  \readlist*\tlist{\mylist}
  \foreachitem\z\in\tlist[]{%
    \expandafter\setsepchar\expandafter{\z}%
    \readlist*\hlist{\myHighlights}
    \ifnum\listlen\hlist[]>1\relax
      \xdef\highlightedProjectList{\highlightedProjectList \z, }%
    \else
      \xdef\normalProjectList{\normalProjectList \z, }%
    \fi
  }
  %if all projects are highlighted
  \ifx\normalProjectList\empty
    \fancyhead[L]{Project Records}
    \foreach\x in \mylist {%    
      \edef\projectNumber{\x}%
      \edef\FileName{projects/project_\x}%     
      \IfFileExists{\FileName}{%  
      \newpage%
      \input{\FileName}%       
      }
    }
  \else
      \foreach\x in \highlightedProjectList {%  
        \edef\projectNumber{\x}%
        \edef\FileName{projects/project_\x}%    
        \IfFileExists{\FileName}{%  
          \fancyhead[L]{Highlighted Project}
          \newpage%
          \input{\FileName}%  
        }
      }
      \foreach\x in \normalProjectList {%   
        \edef\projectNumber{\x}%
        \edef\FileName{projects/project_\x}%    
        \IfFileExists{\FileName}{%  
        \fancyhead[L]{Other Projects}

          \newpage%
          \input{\FileName}%       
        }
      }
    \fi
 \fi
\end{document}

谢谢您的帮助 - 圣诞快乐 :-D

答案1

你必须确保

  1. 使用 更改标题时,LaTeX 位于正确的页面上\fancyhead。因此,请确保在每个命令\newpage前给出\fancyhead
  2. 页面完成之后,另一个标题更改才能进入。因此,我\newpage在每个\input命令后都添加了一个。这在本例中尤其重要,因为有些命令\fancyhead是在 TeX 组中给出的,当该组完成时,原始\lhead{title}命令就会启动。因此,我们希望在发生这种情况之前完成页面。
  • 请注意,后面无需放置%标志\newpage。另一方面,它们不会造成伤害。
  • 为了保持一致性,我将弃用的\lhead命令更改为\fancyhead[L]
\documentclass[10pt]{article}
\usepackage[export]{adjustbox}
\usepackage[a4paper, left=2cm,top=0.8cm,right=2cm,bottom=3.3cm,headheight=61pt, footskip=61pt, includehead]{geometry}
\usepackage{wrapfig}
\usepackage[default]{opensans}
\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}
\usepackage[english,ngerman]{babel}
\usepackage{fancyhdr}
\usepackage{tikz}
\usepackage{xifthen}
\usepackage{listofitems}

\fancypagestyle{plain}{%
  \renewcommand{\headrulewidth}{0pt}%
  \fancyhf{}%
  \fancyhead[L]{title}
}
\pagestyle{plain} % so LaTeX updates the definition
\def\myHighlights{03}   
\def\myList{01, 02, 03, 04, 05, 06, 07, 08, 09, 10, 11, 12, 13, 14, 15,
  16, 17, 18, 19, 20}


\begin{document}
%invert sort (i need it because of the history of the projects)
\let\mylist\empty
  \foreach\x in \myList {
    \ifx\mylist\empty
      \xdef\mylist{\x}%
      \else
      \xdef\mylist{\x,\mylist}%
    \fi
  }

\newcommand\normalProjectList{}
\newcommand\highlightedProjectList{}
 
  %if I don't have highlights:
\ifx\myHighlights\empty
  \newpage
  \fancyhead[L]{Project records}
  \foreach\x in \mylist {%  
    \edef\projectNumber{\x}%
    \edef\FileName{projects/project_\x}%     
    \IfFileExists{\FileName}{%  
       \input{\FileName}
       \newpage
    }
  }
\else
  \readlist*\tlist{\mylist}
  \foreachitem\z\in\tlist[]{%
    \expandafter\setsepchar\expandafter{\z}%
    \readlist*\hlist{\myHighlights}
    \ifnum\listlen\hlist[]>1\relax
      \xdef\highlightedProjectList{\highlightedProjectList \z, }%
    \else
      \xdef\normalProjectList{\normalProjectList \z, }%
    \fi
  }
  %if all projects are highlighted
%  Normal Project list = \Projectlist \par
%  Highlighted list = \highlightedProjectList \par
  \ifx\normalProjectList\empty
    \newpage
    \fancyhead[L]{Project Records}
    \foreach\x in \mylist {%    
      \edef\projectNumber{\x}%
      \edef\FileName{projects/project_\x}%     
      \IfFileExists{\FileName}{%  
      \input{\FileName}
      \newpage
      }
    }
  \else
      \foreach\x in \highlightedProjectList {%  
        \edef\projectNumber{\x}%
        \edef\FileName{projects/project_\x}%    
        \IfFileExists{\FileName}{%  
          \newpage
          \fancyhead[L]{Highlighted Project}
          \input{\FileName}
          \newpage
        }
      }
      \foreach\x in \normalProjectList {%   
        \edef\projectNumber{\x}%
        \edef\FileName{projects/project_\x}%    
        \IfFileExists{\FileName}{%  
          \newpage
          \fancyhead[L]{Other Projects}
          \input{\FileName}
          \newpage
        }
      }
    \fi
 \fi
\end{document}

在此处输入图片描述

相关内容