GeneralIfthen leap years

LaTeX specific issues not fitting into one of the other forums of this category.
Post Reply
tardis93
Posts: 7
Joined: Thu Dec 27, 2012 7:39 pm

Ifthen leap years

Post by tardis93 »

Hello, everybody. I have a problem and I hope someone can help me.can someone tell me how can I list all leap years from 0-2012, in other words I have to write every fourth number from 0-2012, using ifthen package. Also I have to color every second number in specific color. I will be very gratefull if someone help me, and coloring isn't priority. Thank you. :)

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

cgnieder
Site Moderator
Posts: 2000
Joined: Sat Apr 16, 2011 7:27 pm

Ifthen leap years

Post by cgnieder »

Hi tardis93,

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}
Regards
site moderator & package author
tardis93
Posts: 7
Joined: Thu Dec 27, 2012 7:39 pm

Ifthen leap years

Post by tardis93 »

Thanks very much @cgnieder, but I have to do it with ifthen package, that's how professor told us we do. Also, here is the picture how it should look
leaps.png
leaps.png (242.87 KiB) Viewed 5884 times
Last edited by cgnieder on Fri Dec 28, 2012 7:11 pm, edited 1 time in total.
User avatar
cgnieder
Site Moderator
Posts: 2000
Joined: Sat Apr 16, 2011 7:27 pm

Ifthen leap years

Post by cgnieder »

tardis93 wrote:I have to do it with ifthen package, that's how professor told us we do.
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.

Regards
site moderator & package author
tardis93
Posts: 7
Joined: Thu Dec 27, 2012 7:39 pm

Ifthen leap years

Post by tardis93 »

It's one of my college courses, and we are studying latex right know. And yes, it is homework and he specificly said we use ifthen package, and I don't know are we supposed to use ifthen command or whiledo, some of my friends are using while do, and they say that they use this syntax \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.
Last edited by cgnieder on Fri Dec 28, 2012 8:02 pm, edited 1 time in total.
User avatar
cgnieder
Site Moderator
Posts: 2000
Joined: Sat Apr 16, 2011 7:27 pm

Ifthen leap years

Post by cgnieder »

Ok then.

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}
Counters can be set explicitly or can be stepped:

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 value of a counter can be retrieved with different methods. There is always a command \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
Knowing this the task becomes:

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}
To get the format in colours and columns one should probably set up a table:

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}
Regards
site moderator & package author
User avatar
cgnieder
Site Moderator
Posts: 2000
Joined: Sat Apr 16, 2011 7:27 pm

Ifthen leap years

Post by cgnieder »

As an addendum: all of this can also be done with etoolbox which would also avoid the need to define a custom conditional. In my opinion this is probably the best way for a beginner:

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}
I hope your professor will tell you about this package as well as about the shortcomings of »ifthen« in one of your next lessons.

Regards
site moderator & package author
tardis93
Posts: 7
Joined: Thu Dec 27, 2012 7:39 pm

Re: Ifthen leap years

Post by tardis93 »

Tnak you very much @cgnieder, this helps a lot :D Actually we finished with our course and we know have homeworks, and exam in a few days. LaTeX is awesome, just our professor was uninterested in helping as and he was arrogant and stuff, and this is first year that our college is giving LaTeX courses.

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 :)
User avatar
cgnieder
Site Moderator
Posts: 2000
Joined: Sat Apr 16, 2011 7:27 pm

Ifthen leap years

Post by cgnieder »

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 [...]
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.

Anyway. A parbox

Code: Select all

\parbox{<width>}{<code>}
allows to typeset <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}
It should be possible to change colors every second time using »ifthen«'s \newboolean, \setboolean and an \ifthenelse test.

Regards
site moderator & package author
tardis93
Posts: 7
Joined: Thu Dec 27, 2012 7:39 pm

Re: Ifthen leap years

Post by tardis93 »

I know that this is wrong approach, our professor has wrong approach on entire course, we didn't have any exercises. He would just come, show us some slides, some examples on slides and give us homework. And on midterm he gave us to write LaTeX code on paper even we didn't have any exercise. For example, he gave us the picture of table and we had to write a code of that table on piece of paper. And I think i would rather send him homework using tabular than parbox, it's easier and we just had to replicate his page, the parbox thing was just like a hint. Thank you very much and I hope that you will be here if i need help some other time.
Post Reply