0

Based on my question "Check if number is in list" I have another problem:

I used the answer from Steven to get two lists: highlightedProjects and normalProjects. The projects are tex-Files in a project-folder named like "project_" + a number asc. from 01. Now, I try first to get all projects from highlightedProjects and give them the header "Highlighted Project". After all highlighted projects I want all other projects from normalProjects with the header "Other Projects". But the result from my example code below has the wrong headers. The order is correct, the first project is project 03 but with the header "Other Projects". The other projects have the same header excepted the last one. It has the default header "title"?? What am I doing wrong?

Here my code:

\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}

Thanks for help - and merry christmas :-D

1 Answers1

1

You must make sure that

  1. LaTeX is on the correct page when the header is changed with \fancyhead. Therefore make sure that a \newpage is given before each \fancyhead command
  2. the page is finished before another header change can come in. Therefore I added a \newpage after each \input command. This is especially important in this example because some of the \fancyhead commands are given in a TeX group, and when the group finishes the original \lhead{title} kicks in. So we want to finish the page before that happens.
  • Please note that there is no need to put % signs after \newpage. On the other hand they don't harm.
  • For consistency I changed the deprecated \lhead commands to \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}

enter image description here