Graphics, Figures & TablesAutomatic Calculation of Week Number in Table

Information and discussion about graphics, figures & tables in LaTeX documents.
User avatar
Stefan Kottwitz
Site Admin
Posts: 10335
Joined: Mon Mar 10, 2008 9:44 pm

Automatic Calculation of Week Number in Table

Post by Stefan Kottwitz »

Just a remark, in the example babel was called without language option, which prevents compilation. I just noticed it when I used the convenient online compiler option (well, the mentioned slashbox too).

Stefan
LaTeX.org admin

Recommended reading 2024:

LaTeXguide.org • LaTeX-Cookbook.net • TikZ.org

NEW: TikZ book now 40% off at Amazon.com for a short time.

And: Currently, Packt sells ebooks for $4.99 each if you buy 5 of their over 1000 ebooks. If you choose only a single one, $9.99. How about combining 3 LaTeX books with Python, gnuplot, mathplotlib, Matlab, ChatGPT or other AI books? Epub and PDF. Bundle (3 books, add more for higher discount): https://packt.link/MDH5p

svend_tveskaeg
Posts: 478
Joined: Sun Jul 12, 2009 5:31 am

Automatic Calculation of Week Number in Table

Post by svend_tveskaeg »

Arrrh, yes. Initially, I parsed the language option the \documentclass but then removed it again and forgot to parse it to babel.
``In the game of chess, you can never let your adversary see your pieces.''
-- Zapp Brannigan, Futurama (season 1, episode 4)
User avatar
cgnieder
Site Moderator
Posts: 2000
Joined: Sat Apr 16, 2011 7:27 pm

Automatic Calculation of Week Number in Table

Post by cgnieder »

svend_tveskaeg wrote:I am not sure how to combine the two solution, in order to get yours but with week numbers modulo 52.
I haven't wuite understoof what you exactly want to calculate how. (maybe I'm too tired and should go to bed...) Can you give me a pseudo-code example?

Regards
site moderator & package author
User avatar
svend_tveskaeg
Posts: 478
Joined: Sun Jul 12, 2009 5:31 am

Automatic Calculation of Week Number in Table

Post by svend_tveskaeg »

cgnieder wrote:Can you give me a pseudo-code example?
Sure!

Pseudo input:

Code: Select all

\documentclass{article}

\newcounter{week}
\setcounter{week}{49}

\newcommand\week[1]{Week~<CODE>}

\begin{document}

\begin{tabular}{lccccc}
 & \week{1} & \week{2} & \week{3} & \week{4} & \week{5} \\
 & \week{2} & \week{3} & \week{4} & \week{5} & \week{1} \\
 & \week{3} & \week{4} & \week{5} & \week{1} & \week{2} \\
 & \week{4} & \week{5} & \week{1} & \week{2} & \week{3} \\
 & \week{5} & \week{1} & \week{2} & \week{2} & \week{4}
\end{tabular}

\end{document}
Output:

Code: Select all

Week 49  Week 50  Week 51  Week 52  Week 1
Week 50  Week 51  Week 52  Week 1   Week 49
Week 51  Week 52  Week 1   Week 49  Week 50
Week 52  Week 1   Week 49  Week 50  Week 51
Week 1   Week 49  Week 50  Week 51  Week 52
Even better if the numbers can be aligned after the first decimal, i.e.,

Code: Select all

52
 1
but that is by no means important at all.
``In the game of chess, you can never let your adversary see your pieces.''
-- Zapp Brannigan, Futurama (season 1, episode 4)
User avatar
cgnieder
Site Moderator
Posts: 2000
Joined: Sat Apr 16, 2011 7:27 pm

Automatic Calculation of Week Number in Table

Post by cgnieder »

Ah, ok. Pretty straight forward, I would think:

Code: Select all

\documentclass{article}

\newcounter{week}
\setcounter{week}{49}

% \modulo by @egreg (http://tex.stackexchange.com/a/34449/5049)
\def\truncdiv#1#2{((#1-(#2-1)/2)/#2)}
\def\moduloop#1#2{(#1-\truncdiv{#1}{#2}*#2)}
\def\modulo#1#2{\number\numexpr\moduloop{#1}{#2}\relax}

\def\weeknum#1{\the\numexpr\modulo{#1+\value{week}-1}{52}\relax}

\newcommand\week[1]{week~\ifnum\weeknum{#1}=0\relax52\else\weeknum{#1}\fi}

\begin{document}

\begin{tabular}{lccccc}
 & \week{1} & \week{2} & \week{3} & \week{4} & \week{5} \\
 & \week{2} & \week{3} & \week{4} & \week{5} & \week{1} \\
 & \week{3} & \week{4} & \week{5} & \week{1} & \week{2} \\
 & \week{4} & \week{5} & \week{1} & \week{2} & \week{3} \\
 & \week{5} & \week{1} & \week{2} & \week{3} & \week{4}
\end{tabular}

\end{document}
Regards
site moderator & package author
User avatar
svend_tveskaeg
Posts: 478
Joined: Sun Jul 12, 2009 5:31 am

Re: Automatic Calculation of Week Number in Table

Post by svend_tveskaeg »

Spot on!

Thank you very much, Clemens.
``In the game of chess, you can never let your adversary see your pieces.''
-- Zapp Brannigan, Futurama (season 1, episode 4)
Post Reply