Text FormattingSuccess: print age / years old

Information and discussion about LaTeX's general text formatting features (e.g. bold, italic, enumerations, ...)
Post Reply
west.logan
Posts: 87
Joined: Tue Sep 14, 2010 6:58 pm

Success: print age / years old

Post by west.logan »

Though most people post questions, I don't suppose it would be too much to share bits of code as well.

I was writing up a will and decided that I wanted to automate certain things. The year is easy to do of course, but I figured there should be something for my age too (I am xx years old). After snooping around a bit and looking at packages which seemed far too complicated (besides, I'm a code minimalist and don't like adding tons of packages), I came up with the following, which I think works fairly well.

Code: Select all

%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}{\the\year}\addtocounter{age}{-\thedobyear}
%condition to test for birthmonth, then birth day. 
\ifnum\the\month>\thedobmonth\else%
	\ifnum\the\day<\thedobday\addtocounter{age}{-1}\fi\fi
Not an incredible story perhaps, but I was pleased to have something that incorporates a few things I'd learned in the TeXBook as well as picked up other places.

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

Juanjo
Posts: 657
Joined: Sat Jan 27, 2007 12:46 am

Success: print age / years old

Post by Juanjo »

Not incredible, but funny :D
Anyway, I think that your code would fail by several reasons. Firstly, you should compare days only if month=dobmonth. Imagine that today is 2011/02/28. Your algorithm would yield that you are 26 years old: once compared month and dobmonth, since day=28>24=dobday, the macro \addtocounter{age}{-1} would not be executed.

Likewise, suppose you insert somewhere the macro \renewcommand{\thedobyear}{\Roman{dobyear}}. Then, the comparisons involving \thedobyear would fail. Take into account that \the<counter> serves to print the value of <counter> according to a given format (arabic, Roman numeral...) The value of <counter> for the purpose of computations is given by \value{counter}.

Please, check if the following code works:

Code: Select all

\documentclass{article}
\usepackage[english]{babel}
\begin{document}
\newcounter{Age}
\newcommand{\WhichAge}[3]{\setcounter{Age}{\numexpr(\month-#2)*100+\day-#3}%
  \ifnum\value{Age}<0%
     \setcounter{Age}{\numexpr\year-#1-1}%
  \else%
     \setcounter{Age}{\numexpr\year-#1}%
  \fi%
  \theAge}

It is \today. Today I am \WhichAge{1985}{3}{24} years old. 
My sister is \WhichAge{1990}{1}{12} years old. 
My brother is \WhichAge{2001}{2}{18} years old.
My Dad is \WhichAge{1951}{7}{22} years old.
And my Mum is \WhichAge{1956}{10}{9} years old.
\end{document}
The CTAN lion is an artwork by Duane Bibby. Courtesy of www.ctan.org.
west.logan
Posts: 87
Joined: Tue Sep 14, 2010 6:58 pm

Re: Success: print age / years old

Post by west.logan »

I did test my code again and you were right about my code failing on specific dates. Thank you for the help. Your example makes use of some commands I was not familiar with and is very instructive.

You may say it's funny but I was just trying to help others who might want to do the same thing.
User avatar
Juanjo
Posts: 657
Joined: Sat Jan 27, 2007 12:46 am

Success: print age / years old

Post by Juanjo »

west.logan wrote: You may say it's funny but I was just trying to help others who might want to do the same thing.
Well, perhaps funny is not the right word (English is not my mother tongue). I meant it was something unusual, curious, something that interested me and I had fun thinking about. And of course I also wanted to help others by replying.
The CTAN lion is an artwork by Duane Bibby. Courtesy of www.ctan.org.
west.logan
Posts: 87
Joined: Tue Sep 14, 2010 6:58 pm

Re: Success: print age / years old

Post by west.logan »

Ah, thank you for clarifying. I guessed that perhaps was the case since you used the English option with the Babel package. But once again, it was informative. I wasn't sure how to do mathematical operations on the counters in LaTeX so I just did what I could with what I did know.

It was an enjoyable little diversion :)
Post Reply