Automatically calcultate your age with LaTeX

After unfruitful efforts to find on the internet a simple LaTeX command that would enable me to automatically calcultate my age in my CV, I decided to write my own macro. It is rather simple and only requires the xifthen package.

My method sets a number of counters corresponding to one’s date of birth, and then compares them to the local system time retrieved by LaTeX. The age itself is calculated with an age counter which can be printed out with the command \theage. Please let me know if you it doesn’t work for you.

Here is the macro:

\usepackage{xifthen}

\newcounter{dobyear}\setcounter{dobyear}{
}
%fill in your birth’s year above
\newcounter{dobmonth}\setcounter{dobmonth}{ }
%fill in your birth’s month in numeric value above
\newcounter{dobday}\setcounter{dobday}{ }
%fill in your birth’s day above
\newcounter{age}\setcounter{age}{\the\year\thedobyear}
%this compares your’s birth year to the current year and stores the value in a new counter
\newtest{\conditiona}{%
\cnttest{\the\month}{=}{\thedobmonth} \AND %
\cnttest{\the\day}{>=}{\thedobday}%
}
%this sets the condition “if the current month is the same as your birth month and the current day is greater or equal than your birth day
\newtest{\conditionb}{%
\cnttest{\the\month}{>}{\thedobmonth}%
}
%this sets the  condition: “if we the current month is after your birth month“
\ifthenelse{\conditiona \OR \conditionb}{}{\addtocounter{age}{-1}}
%this adjusts your age if one of the two conditions above is true, i.e. current time is later than your birthday of this year.

Now a minimal working example:

\documentclass{article}
\usepackage{xifthen}
\newcounter{dobyear}\setcounter{dobyear}{1979}
\newcounter{dobmonth}\setcounter{dobmonth}{09}
\newcounter{dobday}\setcounter{dobday}{16}
\newcounter{age}\setcounter{age}{\the\year\thedobyear}
\newtest{\conditiona
}{%
\cnttest{\the\month}{=}{\thedobmonth} \AND %
\cnttest{\the\day}>=}{\thedobday}%
}
\newtest{\conditionb}{%
\cnttest{\the\month}{>}{\thedobmonth}%
}
\ifthenelse{\conditiona \OR \conditionb}{}{\addtocounter{age}{–1}}
\begin{document}

Thomas Pellard is \theage\ years old today.
\end{document
}
This entry was posted in LaTeX. Bookmark the permalink.

2 Responses to Automatically calcultate your age with LaTeX

  1. Logan West says:

    I wanted to do something similar and came up with a simpler method, using basic TeX commands:

    %counters to calculate my age.
    %Place ‘theage’ in the document where you want your age to appear.
    %year counter, month counter, and day counter
    newcounter{dobyear}setcounter{dobyear}{1985}
    newcounter{dobmonth}setcounter{dobmonth}{3}
    newcounter{dobday}setcounter{dobday}{24}
    %calculate number of years
    newcounter{age}setcounter{age}{theyear}addtocounter{age}{-thedobyear}
    %condition to test for birthmonth, then birth day.
    ifnumthemonth>thedobmonthelse%
    ifnumtheday<thedobdayaddtocounter{age}{-1}fifi

  2. Logan West says:

    MWE:

    documentclass{article}

    %counters to calculate my age.
    %Place ‘theage’ in the document where you want your age to appear.
    %year counter, month counter, and day counter
    newcounter{dobyear}setcounter{dobyear}{1985}
    newcounter{dobmonth}setcounter{dobmonth}{3}
    newcounter{dobday}setcounter{dobday}{24}
    %calculate number of years
    newcounter{age}setcounter{age}{theyear}addtocounter{age}{-thedobyear}
    %condition to test for birthmonth, then birth day.
    ifnumthemonth>thedobmonthelse%
    ifnumtheday<thedobdayaddtocounter{age}{-1}fifi

    begin{document}
    I am theage years old today.
    end{document}

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>