
General ⇒ Ifthen leap years
Ifthen leap years

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
Ifthen leap years
welcome to the LaTeX community!
Here is an idea without any package (except for xcolor):
Code: Select all
\documentclass{article}
\usepackage{xcolor}
\colorlet{firstnumber}{black}
\colorlet{secondnumber}{red}
\makeatletter
\newif\if@secondnumber
\newcounter{mycounter}
\newcommand\leaps[1]{%
\setcounter{mycounter}{0}%
\@leaps{#1}%
}
\newcommand\@leaps[1]{%
\if@secondnumber
\textcolor{secondnumber}{\arabic{mycounter}}
\@secondnumberfalse
\else
\textcolor{firstnumber}{\arabic{mycounter}}
\@secondnumbertrue
\fi
\addtocounter{mycounter}{4}%
\ifnum\value{mycounter}<\numexpr#1+1\relax
\@leaps{#1}%
\fi
}
\makeatother
\begin{document}
\leaps{10}
\leaps{2012}
\end{document}
Ifthen leap years
Ifthen leap years
Are you taking a LaTeX course and this is some kind of homework? If it's not there is no reason at all for using the »ifthen« package. There is little one can do with it that can't be done better with other packages.tardis93 wrote:I have to do it with ifthen package, that's how professor told us we do.
Regards
Ifthen leap years
\whiledo{\value{countersname}<2013}}
, and i honestly don't understand how to do it, especially because I didn't understood counters very well, and I cant find any tutorials for them online.Ifthen leap years
Here are some information about counters:
a counter is LaTeX construct that holds an integer value. They are used in various places. For example section numbers or the page number are stored in counters.
Counters are allocated with
Code: Select all
\newcounter{foo}
Code: Select all
\setcounter{foo}{<num>}% sets foo to <num>
\addtocounter{foo}{<num>}% adds <num> to the value of foo
\stepcounter{foo}% steps foo by 1
\refstepcounter{foo}% steps foo by one and makes the numbers accessible for the \label/\ref mechanism
\the<counter>
for each counter. For example after saying \newcounter{foo}
a command \thefoo
will be available for typesetting the counter value. Its format depends on the current definition of \thefoo
which means this command can be used for customized output. There are also a few more specific commands:
Code: Select all
\arabic{foo}% value as arabic number
\alph{foo}% value as lowercase letter
\Alph{foo}% value as uppercase letter
\roman{foo}% value as lowercase roman numeral
\Roman{foo}% value as uppercase roman numeral
\value{foo}% value for use in calculations
Code: Select all
\documentclass{article}
\usepackage{ifthen}
% allocate counter:
\newcounter{mycounter}
% just to be safe:
\setcounter{mycounter}{0}
\begin{document}
\whiledo{\value{mycounter}<2013}{% loop while the value is less than 2013
\arabic{mycounter} % typeset the value
\addtocounter{mycounter}{4}% add 4 to the current value
}
\end{document}
Code: Select all
\documentclass{article}
\usepackage{ifthen}
\usepackage{array,xcolor}
% allocate counter:
\newcounter{mycounter}
\newcounter{column}
% just to be safe:
\setcounter{mycounter}{0}
% make @ a letter so it can be used in a command name:
\makeatletter
% Since we can't use \ifthenelse{}{}{} to insert & or \\ conditionally as
% \ifthenelse isn't expandable let's define our own conditional.
% \@firstoftwo takes two arguments and outputs the first
% \@secondoftwo takes two arguments and outputs the second
\newcommand\ifcolumn[1]{%
\ifnum\value{column}<\numexpr#1-1\relax
% use \expandafter to safely finish the conditional
% first and then use the second argument that follows
\expandafter\@secondoftwo
\else
% use \expandafter to safely finish the conditional
% first and then use the first argument that follows
\expandafter\@firstoftwo
\fi
}
% make @ other again:
\makeatother
\begin{document}
% smaller font size so everything fits on one page
\footnotesize
\begin{tabular}{*{5}{r>{\color{red}}r}}% 5 times one black and one red column
\whiledo{\value{mycounter}<2013}{%
\arabic{mycounter}
\addtocounter{mycounter}{4}%
\ifcolumn{10}
{\setcounter{column}{0}\\}
{\stepcounter{column}&}
}
\end{tabular}
\end{document}
Ifthen leap years
Code: Select all
\documentclass{article}
\usepackage{etoolbox}
\usepackage{array,xcolor}
% allocate counter:
\newcounter{mycounter}
\newcounter{column}
% set the counters:
\setcounter{mycounter}{0}
\setcounter{column}{1}
\begin{document}
% smaller font size so everything fits on one page
\footnotesize
\begin{tabular}{*{5}{r>{\color{red}}r}}% 5 times one black and one red column
\whileboolexpr{ test {\ifnumless{\value{mycounter}}{2013}} }{%
\arabic{mycounter}
\addtocounter{mycounter}{4}%
\ifnumequal{\value{column}}{10}
{\setcounter{column}{1}\\}
{\stepcounter{column}&}
}
\end{tabular}
\end{document}
Regards
Re: Ifthen leap years

And, just for information, how can we do this with parbox. Our professor gave us something like hint that we could use parbox with width of 10mm to do this, and i'm curious how can he do that. Does he need to use the multicolumn enviroment and multiple parboxes or can we do that with just one parbox? Thanks again

Ifthen leap years
I was afraid of something like this. It is clearly the wrong approach. The image you provided shows a table so one really should use a table. One of LaTeX's strengths is the semantically meaningful markup.tardis93 wrote:[...] how can we do this with parbox. Our professor gave us something like hint that we could use parbox with width of 10mm to do this [...]
Anyway. A parbox
Code: Select all
\parbox{<width>}{<code>}
<code>
that may contain paragraphs in a box of a given <width>
. So typesetting the value of the counter in the loop each time in a parbox would simulate the tabular like appearance. The idea is something like this:Code: Select all
\documentclass{article}
\usepackage{ifthen}
% allocate counter:
\newcounter{mycounter}
\setcounter{mycounter}{0}
\begin{document}
\noindent
\whiledo{\value{mycounter}<2013}{% loop while the value is less than 2013
% typeset the value:
\parbox{7.5mm}{%
\hfill% shift everything to the right
\arabic{mycounter}%
}
% add 4 to the current value:
\addtocounter{mycounter}{4}%
}
\end{document}
\newboolean
, \setboolean
and an \ifthenelse
test.Regards