Home > Enterprise >  LaTeX function to determine Calendar Quarter for last month and the Quarter 2 years prior to it
LaTeX function to determine Calendar Quarter for last month and the Quarter 2 years prior to it

Time:11-16

I have a simple document like the following:

\documentclass[11pt]{article}
\usepackage{graphicx}
\begin{document}
\pagebreak
\hspace{1cm}\large\textbf{Title}

\begin{center}
   \includegraphics[scale=.75]{./graph.png}
\end{center}
\end{document}

I'm trying to find a way to change my graph title from 'Title' to 'Title, Q4 2020 - Q4 2022', but using a function that ensures that the first quarter is always the quarter 2 years ago from last month, and the second quarter is always the quarter from last month. So, for example, if I compiled this document in January 2023 it would need to stay Q4 2020 - Q4 2022, but if I ran it in February 2023 I would need it to change to Q1 2021 - Q1 2023.

I don't know how to make complex functions in LaTeX that reference the current date, and I can't find a lot of information on it online. Any help would be greatly appreciated!

CodePudding user response:

\documentclass{article}

\newcommand{\insertquarter}{%
  Q%
  \ifcase\month\or4\or1\or1\or1\or2\or2\or2\or3\or3\or3\or4\or4\fi
}

\newcommand{\foo}{%
  \ifnum\month=1
    \insertquarter\ \number\numexpr\year-3\relax\ -- \insertquarter\ \number\numexpr\year-1\relax 
  \else%
    \insertquarter\ \number\numexpr\year-2\relax\ -- \insertquarter\ \number\year
  \fi%
} 

\usepackage{graphicx}
\begin{document}
\pagebreak
\hspace{1cm}\large\textbf{Title \foo}

\begin{center}
   \includegraphics[scale=.75]{example-image-duck}
\end{center}
\end{document}
  • Related