4

I'm using the report format and would like to a new date to appear on the right header of each section (I can input these dates manually). My knowledge is very basic, so far I have managed to set this in the preamble but I only know how to produce one date across the entire document (given below), rather than updating in each section.

\usepackage{fancyhdr}
\pagestyle{fancy}
\fancyhf{}
\fancyhead[R]{\today}

As an overview, my document will be structured like this to record notes from eight meetings, four in each of two chapters.

-Chapter 1
-Section 1.1 (date 1)
-Section 1.2 (date 2)
-Section 1.3 (date 3)
-Section 1.4 (date 4)

-Chapter 2
-Section 2.1 (date 5)
-Section 2.2 (date 6)
-Section 2.3 (date 7)
-Section 2.4 (date 8)

If someone could provide me with some code that I can use at the beginning of each section where I could input the date, I'd be very grateful.

3
  • Hi, do you need ongoing dates (e.g. where the day or month is increased by one every section)? Or you really want to set each date manually? And which documentclass are you using, since most classes define section etc. differently.
    – lukeflo
    Aug 6, 2023 at 16:34
  • Hi @lukeflo, setting each date manually is needed as the meetings are not consistently spaced apart. My document class is report, thanks!
    – Lauren S
    Aug 6, 2023 at 18:21
  • You can use \date{...} to reset \today to be anything. Aug 6, 2023 at 19:16

2 Answers 2

3

I just defined a new command \datesection which takes up to 3 arguments:

\datesection[<short title>]{<section title>}[<date/right header content>]

  • The first 2 (optional and mandatory) are the standard \section arguments
  • The last/trailing optional argument sets the right header

It also has a starred version, which works like the \section* command:

\documentclass[]{report}
\usepackage[T1]{fontenc}

\usepackage{fancyhdr}
\pagestyle{fancy}
\fancyhf{}

\usepackage{blindtext}

\NewDocumentCommand{\datesection}{s O{#3} m O{\empty}}{
    \IfBooleanTF{#1}{
    \section*{#3}
    \fancyhead[R]{#4}
    }{
    \section[#2]{#3}
    \fancyhead[R]{#4}
    }
}

\begin{document}

\datesection{title}[\today]
\blindtext

\clearpage

\datesection[short title]{longer title}[tomorrow]
\blindtext

\clearpage

\datesection*{title 2}[yesterday]
\blindtext

\end{document}

Hope that works for you!

5
  • 1
    Thank you @lukeflo, this is exactly what I was looking for!
    – Lauren S
    Aug 6, 2023 at 19:59
  • I've just noticed that this changes the section names in the table of contents to "-NoValue-" instead of the section title, would there be an easy way to fix this?
    – Lauren S
    Aug 6, 2023 at 20:21
  • Oh, didn't check the TOC. I guess it occurs if you leave the first optional argument empty. Right now I've no access to my PC. Will check it ASAP.
    – lukeflo
    Aug 6, 2023 at 20:38
  • Ah silly me - you're spot on, thanks!
    – Lauren S
    Aug 6, 2023 at 20:41
  • 1
    @LaurenS, no, you were right. Didn't thought about that xparse inserts -NoValue- for empty optional arguments. Fixed the NewDocumentCommand. Now it works with empty optional arguments.
    – lukeflo
    Aug 6, 2023 at 20:53
2

You can define a variable by using \def\<variablename> And then just update the variable with each new section.

Here is an MWE:

\documentclass{article}

\begin{document}

\def\kdate{4}

\section{Section 1.1  - \kdate}

\def\kdate{5}

\section{Section 1.2  - \kdate}

\def\kdate{6}

\section{Section 1.3  - \kdate}


\end{document}

I've used kdate for a variable name. You can use whatever you want, just make sure it doesn't clash with a pre-existing variable or command.

enter image description here

Edit:

If you are using the fancyhdr package to build your headers, you can add that variable to your \fancyhead command:

\pagestyle{fancy}
 \fancyhf{}
 \fancyhead[LE,RO]{\thepage}
 \fancyhead[RE,LO]{\kdate}
3
  • 2
    But the date is not in the header as asked for by the OP.
    – lukeflo
    Aug 6, 2023 at 17:21
  • @lukeflo That's because OP did not provide an MWE showing how they are defining the header. If they are using fancyhdr they can just add their variable to \pagestyle{fancy} in the preamble. But I don't know how they are building their headers. I've updated my response with this example. Aug 6, 2023 at 19:36
  • 1
    Thank you @KennethOdle, your edit is really helpful - much appreciated!
    – Lauren S
    Aug 6, 2023 at 19:57

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .