如何创建循环计数器

如何创建循环计数器

我正在为我的机构准备学术日历。它通常如下所示: 在此处输入图片描述 我将创建一个tabular以适合数据。我已经设法通过计数器生成日期。我现在需要的是循環計數器为天数。我想\day[starting day of the month]在第一行和\day后续行中输入。因此它应该生成整个第二列,如上图所示。请帮助我实现这一点。以下是我正在使用的代码。

\documentclass[9pt, twoside]{scrartcl}
\usepackage[cmyk]{xcolor}
\usepackage[paperwidth=108mm, paperheight=140mm, top=15mm, bottom=15mm, left=15mm, right=15mm, foot=2.5mm, head=2.5mm, showframe, marginparsep=0mm]{geometry}
\usepackage{fontspec}
\setmainfont{Latin Modern Roman}
\setsansfont{Latin Modern Sans}
\renewcommand{\familydefault}{\sfdefault}
\usepackage{titlesec}

\makeatletter
\titleformat{\section}[runin]{}{}{0pt}{\@gobble}
\titleformat{\subsection}[runin]{}{}{0pt}{\@gobble}
\makeatother
\titlespacing{\section}{0pt}{-\baselineskip}{*0}
\titlespacing{\subsection}{0pt}{-\baselineskip}{*0}

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

\renewcommand{\sectionmark}[1]{\markboth{#1}{}}
\renewcommand{\subsectionmark}[1]{\markright{#1}}
\fancyhead[LE]{\color{cyan}\leftmark~|~\lastrightmark~|~Academic Calendar 2022-23}
\fancyhead[RO]{\color{cyan}Academic Calendar 2022-23~|~\lastrightmark~|~\leftmark}
\usepackage{hyperref}
\usepackage{ragged2e}
\usepackage{array, longtable}
\parindent0pt
\parskip0pt
\usepackage{etoolbox}
\newcounter{magicrownumbers}
\newcommand\Rownum{\stepcounter{magicrownumbers}\arabic{magicrownumbers}}
\preto\table{\setcounter{magicrownumbers}{0}}
\preto\tabular{\setcounter{magicrownumbers}{0}}
%
%
%
\begin{document}
\thispagestyle{empty}
    \vspace*{\fill}
    \begin{center}
        {\Large\color{cyan}Academic Calendar\\2022-23}
    \end{center}
    \vfill
    \newpage
\section{July}
\subsection{Sonada}
%
    \begin{center}
        \begin{tabular}{rlll}
            \Rownum.    & Mon & Type & Content\\ %Want to replace "Mon" by "\day[starting day of the month]"
            \Rownum.    & Tue & Type & Content %Want to replace "Tue" by "\day"
        \end{tabular}
    \end{center}
    \newpage
\subsection{Siliguri}
%
    \begin{center}
        \begin{tabular}{rlll}
            \Rownum.    & Mon & Type & Content\\ %Want to replace "Mon" by "\day[starting day of the month]"
            \Rownum.    & Tue & Type & Content %Want to replace "Tue" by "\day"
        \end{tabular}
    \end{center}
%
\end{document}

附言:如果您能指导我如何将前 9 个日期输入为“01”到“09”,我将不胜感激。目前它打印“1”到“9”。

答案1

您还可以使用pgfcalendar

\documentclass[9pt, twoside]{scrartcl}
\usepackage[cmyk]{xcolor}
\usepackage[paperwidth=108mm, paperheight=140mm, top=15mm, bottom=15mm, left=15mm, right=15mm, foot=2.5mm, head=2.5mm, showframe, marginparsep=0mm]{geometry}
\usepackage{fontspec}
\setmainfont{Latin Modern Roman}
\setsansfont{Latin Modern Sans}
\renewcommand{\familydefault}{\sfdefault}
\usepackage{titlesec}

\makeatletter
\titleformat{\section}[runin]{}{}{0pt}{\@gobble}
\titleformat{\subsection}[runin]{}{}{0pt}{\@gobble}
\makeatother
\titlespacing{\section}{0pt}{-\baselineskip}{*0}
\titlespacing{\subsection}{0pt}{-\baselineskip}{*0}

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

\renewcommand{\sectionmark}[1]{\markboth{#1}{}}
\renewcommand{\subsectionmark}[1]{\markright{#1}}
\fancyhead[LE]{\color{cyan}\leftmark~|~\lastrightmark~|~Academic Calendar 2022-23}
\fancyhead[RO]{\color{cyan}Academic Calendar 2022-23~|~\lastrightmark~|~\leftmark}
\usepackage{hyperref}
\usepackage{ragged2e}
\usepackage{array, longtable}
\parindent0pt
\parskip0pt
\usepackage{etoolbox}
\newcounter{magicrownumbers}
\newcommand\Rownum{\stepcounter{magicrownumbers}\ifnum\value{magicrownumbers}<10 0\fi\arabic{magicrownumbers}}
\preto\table{\setcounter{magicrownumbers}{0}}
\preto\tabular{\setcounter{magicrownumbers}{0}}
%
%
%
\newcount\myjdate
\newcount\myweekday
\newcounter{tmpmyjdate}
\newcounter{tmpmyweekday}
\usepackage{pgfcalendar}
\def\wday#1{%
\pgfcalendardatetojulian{#1}{\myjdate}%
\setcounter{tmpmyjdate}{\the\myjdate}%
\pgfcalendarjuliantoweekday{\thetmpmyjdate}{\myweekday}%
\setcounter{tmpmyweekday}{\the\myweekday}%
\nwday%
}
\def\nwday{%
\ifcase\value{tmpmyweekday} Mon\or Tue\or Wen\or Thu\or Fri\or Sat\or Sun\fi\relax%
\stepcounter{tmpmyjdate}\relax%
\pgfcalendarjuliantoweekday{\thetmpmyjdate}{\myweekday}%
\setcounter{tmpmyweekday}{\the\myweekday}%
}

\begin{document}
\thispagestyle{empty}
    \vspace*{\fill}
    \begin{center}
        {\Large\color{cyan}Academic Calendar\\2022-23}
    \end{center}
    \vfill
    \newpage
\section{July}
\subsection{Sonada}
    \begin{center}
    \begin{tabular}{rlll}
            \Rownum.    & \wday{2022-07-01} & Type & Content\\ 
            \Rownum.    & \nwday & Type & Content\\ 
            \Rownum.    & \nwday & Type & Content\\ 
            \Rownum.    & \nwday & Type & Content\\ 
            \Rownum.    & \nwday & Type & Content\\ 
            \Rownum.    & \nwday & Type & Content\\ 
            \Rownum.    & \nwday & Type & Content\\ 
            \Rownum.    & \nwday & Type & Content\\ 
            \Rownum.    & \nwday & Type & Content\\ 
            \Rownum.    & \nwday & Type & Content\\ 
            \Rownum.    & \nwday & Type & Content\\ 
            \Rownum.    & \nwday & Type & Content 
        \end{tabular}
    \end{center}
    \newpage
\subsection{Siliguri}
%
    \begin{center}
        \begin{tabular}{rlll}
            \Rownum.    & \wday{2022-07-01} & Type & Content\\ 
            \Rownum.    & \nwday & Type & Content\\ 
            \Rownum.    & \nwday & Type & Content\\ 
        \end{tabular}
    \end{center}
%
\end{document}

答案2

在这个演示中,我只给出了一周中的 3 天,而不是 7 天。请根据需要进行修改。

混合使用 expl3 编程和纯 TeX 编程(不用说,计数器还使用 LaTeX2 风格(这叫什么?)编程)。阅读 interface3.pdf 和 TeXbook/TeX by Topic 以及 LaTeX 配套/非官方 LaTeX2e 参考手册,分别了解命令的文档(或者在网站上搜索,尽管有些人可能没有)。

另外 LaTeX 已经有一个命令,我在这里\day使用\dayz

%! TEX program = lualatex

% ** search for "begin insertion" below to see the code. OP gives MWE too long...

\documentclass[9pt, twoside]{scrartcl}
\usepackage[cmyk]{xcolor}
\usepackage[paperwidth=108mm, paperheight=140mm, top=15mm, bottom=15mm, left=15mm, right=15mm, foot=2.5mm, head=2.5mm, showframe, marginparsep=0mm]{geometry}
\usepackage{fontspec}
\setmainfont{Latin Modern Roman}
\setsansfont{Latin Modern Sans}
\renewcommand{\familydefault}{\sfdefault}
\usepackage{titlesec}

\makeatletter
\titleformat{\section}[runin]{}{}{0pt}{\@gobble}
\titleformat{\subsection}[runin]{}{}{0pt}{\@gobble}
\makeatother
\titlespacing{\section}{0pt}{-\baselineskip}{*0}
\titlespacing{\subsection}{0pt}{-\baselineskip}{*0}

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

\renewcommand{\sectionmark}[1]{\markboth{#1}{}}
\renewcommand{\subsectionmark}[1]{\markright{#1}}
\fancyhead[LE]{\color{cyan}\leftmark~|~\lastrightmark~|~Academic Calendar 2022-23}
\fancyhead[RO]{\color{cyan}Academic Calendar 2022-23~|~\lastrightmark~|~\leftmark}
\usepackage{hyperref}
\usepackage{ragged2e}
\usepackage{array, longtable}
\parindent0pt
\parskip0pt
\usepackage{etoolbox}
\newcounter{magicrownumbers}
\newcommand\Rownum{\stepcounter{magicrownumbers}\arabic{magicrownumbers}}
\preto\table{\setcounter{magicrownumbers}{0}}
\preto\tabular{\setcounter{magicrownumbers}{0}}
%
%
%
\begin{document}
\thispagestyle{empty}
    \vspace*{\fill}
    \begin{center}
        {\Large\color{cyan}Academic Calendar\\2022-23}
    \end{center}
    \vfill
    \newpage
\section{July}


% ======== begin insertion
\ExplSyntaxOn
\newcounter{dayofweek}  % 0: sun, 1: mon, ...
\NewDocumentCommand \dayz {o} {
    \IfValueTF {#1} {
        \setcounter {dayofweek} {
            \str_case:nn {#1} {
                {Sun} {0}
                {Mon} {1}
                {Tue} {2}
            }
        }
    } {
        \stepcounter{dayofweek}
        \ifnum \value{dayofweek} = 3
            \setcounter{dayofweek} {0}
        \fi  % wrap around 3 → 0.
    }

    \ifcase \value{dayofweek}
        Sun \or Mon \or Tue  % just for demo.
    \fi
}

\ExplSyntaxOff
% ======== end insertion



\subsection{Sonada}
%
    \begin{center}
        \begin{tabular}{rlll}
            \Rownum.    & \dayz[Mon] & Type & Content\\ %Want to replace "Mon" by "\day[starting day of the month]"
            \Rownum.    & \dayz      & Type & Content %Want to replace "Tue" by "\day"
        \end{tabular}
    \end{center}
    \newpage
\subsection{Siliguri}
%
    \begin{center}
        \begin{tabular}{rlll}
            \Rownum.    & \dayz[Mon]  & Type & Content\\ %Want to replace "Mon" by "\day[starting day of the month]"
            \Rownum.    & \dayz       & Type & Content %Want to replace "Tue" by "\day"
        \end{tabular}
    \end{center}
%
\end{document}

相关内容