| 1 |
% \iffalse meta-comment |
| 2 |
% |
| 3 |
% Copyright (C) 2012 by Robin Schneider <[email protected]> |
| 4 |
% |
| 5 |
% This work may be distributed and/or modified under the |
| 6 |
% conditions of the LaTeX Project Public License, either version 1.3 |
| 7 |
% of this license or (at your option) any later version. |
| 8 |
% The latest version of this license is in |
| 9 |
% http://www.latex-project.org/lppl.txt |
| 10 |
% and version 1.3 or later is part of all distributions of LaTeX |
| 11 |
% version 2005/12/01 or later. |
| 12 |
% |
| 13 |
% This work has the LPPL maintenance status `maintained'. |
| 14 |
% |
| 15 |
% The Current Maintainer of this work is Robin Schneider. |
| 16 |
% |
| 17 |
% This work consists of the files calcage.dtx and calcage.ins |
| 18 |
% and the derived filebase calcage.sty. |
| 19 |
% |
| 20 |
% \fi |
| 21 |
% |
| 22 |
% \iffalse |
| 23 |
%<*driver> |
| 24 |
\ProvidesFile{calcage.dtx} |
| 25 |
%</driver> |
| 26 |
%<package>%% See file 'calcage.dtx' for copyright and licence. |
| 27 |
%<package>\NeedsTeXFormat{LaTeX2e}[1998/12/01] |
| 28 |
%<package>\ProvidesPackage{calcage} |
| 29 |
%<*package> |
| 30 |
[2012/09/09 v0.90 Calculate the age in years] |
| 31 |
%</package> |
| 32 |
% |
| 33 |
%<*driver> |
| 34 |
\documentclass[english]{ltxdoc} |
| 35 |
\newcommand{\PackageURL}{https://github.com/ypid/latex-packages/tree/master/calcage} |
| 36 |
\newcommand{\PackageCTANURL}{http://www.ctan.org/pkg/calcage} |
| 37 |
\newcommand{\PackageAuthor}{Robin Schneider} |
| 38 |
\newcommand{\PackageAuthorEmail}{[email protected]} |
| 39 |
\newcommand{\PackageName}{\PrintPackage{calcage}} |
| 40 |
\newcommand{\PrintPackage}[1]{\textsf{#1}} |
| 41 |
\newcommand{\PrintOptionF}[1]{\emph{#1}} %% ^^A This macro is used for |
| 42 |
%% ^^A explaining any parameter when they first come up in the documentation. |
| 43 |
\newcommand{\DescribePara}[1]{\marginpar{\raggedleft\strut\MacroFont\string #1}} |
| 44 |
\usepackage{calcage} |
| 45 |
\usepackage{ |
| 46 |
babel, |
| 47 |
csquotes, |
| 48 |
xcolor, |
| 49 |
url, |
| 50 |
hypdoc, |
| 51 |
nameref, |
| 52 |
} |
| 53 |
\GetFileInfo{calcage.dtx} |
| 54 |
\hypersetup{ |
| 55 |
pdftitle={A manual for \PackageName}, |
| 56 |
pdfauthor={\PackageAuthor{} <\PackageAuthorEmail>}, |
| 57 |
pdfsubject={\fileinfo}, |
| 58 |
baseurl={\PackageURL}, |
| 59 |
pdfkeywords={This document corresponds to \PackageName~\fileversion, |
| 60 |
dated \filedate} |
| 61 |
} |
| 62 |
|
| 63 |
\title{The \PackageName{} package\thanks{This document |
| 64 |
corresponds to \PackageName~\fileversion, dated \filedate.}} |
| 65 |
\author{\PackageAuthor \\ |
| 66 |
\texttt{\href{mailto:\PackageAuthorEmail?subject=LaTeX package calcage}% |
| 67 |
{\PackageAuthorEmail}% |
| 68 |
}% |
| 69 |
} |
| 70 |
|
| 71 |
\EnableCrossrefs |
| 72 |
\CodelineIndex |
| 73 |
\RecordChanges |
| 74 |
\begin{document} |
| 75 |
\DocInput{calcage.dtx} |
| 76 |
\PrintChanges |
| 77 |
\PrintIndex |
| 78 |
\end{document} |
| 79 |
%</driver> |
| 80 |
% \fi |
| 81 |
% |
| 82 |
% \CheckSum{122} |
| 83 |
% |
| 84 |
% \CharacterTable |
| 85 |
% {Upper-case \A\B\C\D\E\F\G\H\I\J\K\L\M\N\O\P\Q\R\S\T\U\V\W\X\Y\Z |
| 86 |
% Lower-case \a\b\c\d\e\f\g\h\i\j\k\l\m\n\o\p\q\r\s\t\u\v\w\x\y\z |
| 87 |
% Digits \0\1\2\3\4\5\6\7\8\9 |
| 88 |
% Exclamation \! Double quote \" Hash (number) \# |
| 89 |
% Dollar \$ Percent \% Ampersand \& |
| 90 |
% Acute accent \' Left paren \( Right paren \) |
| 91 |
% Asterisk \* Plus \+ Comma \, |
| 92 |
% Minus \- Point \. Solidus \/ |
| 93 |
% Colon \: Semicolon \; Less than \< |
| 94 |
% Equals \= Greater than \> Question mark \? |
| 95 |
% Commercial at \@ Left bracket \[ Backslash \\ |
| 96 |
% Right bracket \] Circumflex \^ Underscore \_ |
| 97 |
% Grave accent \` Left brace \{ Vertical bar \| |
| 98 |
% Right brace \} Tilde \~} |
| 99 |
% |
| 100 |
% \changes{0.90}{2012/09/09}{Initial version} |
| 101 |
% |
| 102 |
% \DoNotIndex{\RequirePackage, \DeclareOption, \ProcessOptions} |
| 103 |
% \DoNotIndex{\PackageWarning, \MessageBreak} |
| 104 |
% \DoNotIndex{\DeclareRobustCommand, \newcommand, \renewcommand, \def, \edef} |
| 105 |
% \DoNotIndex{\DeclareStringOption, \ProcessLocalKeyvalOptions} |
| 106 |
% \DoNotIndex{\ProcessKeyvalOptions, \SetupKeyvalOptions, \DeclareBoolOption} |
| 107 |
% \DoNotIndex{\newenvironment} |
| 108 |
% \DoNotIndex{\if, \else, \fi, \ifcase, \or, \ifthenelse, \AND, \OR, \value, \relax} |
| 109 |
% \DoNotIndex{\loop, \repeat, \the, \ifnum} |
| 110 |
% \DoNotIndex{\equal, \boolean, \@currname, \newcounter, \setcounter} |
| 111 |
% \DoNotIndex{\stepcounter, \addtocounter} |
| 112 |
% \DoNotIndex{\endinput} |
| 113 |
% |
| 114 |
% \maketitle |
| 115 |
% |
| 116 |
% \phantomsection |
| 117 |
% \addcontentsline{toc}{section}{\abstractname} |
| 118 |
% \begin{abstract} |
| 119 |
% The \PackageName{} package can calculate the age in years. \\ |
| 120 |
% Information site on CTAN: \url{\PackageCTANURL} \\ |
| 121 |
% Fork me on GitHub: \url{\PackageURL} \end{abstract} |
| 122 |
% |
| 123 |
% \tableofcontents |
| 124 |
% |
| 125 |
% \section{Introduction} |
| 126 |
% The \PrintPackage{calcage} package can calculate the age of someone or |
| 127 |
% something in years. Internally it uses the \PrintPackage{datenumber} package |
| 128 |
% to calculate the age in days. The conversion from days to years is |
| 129 |
% implemented by this package. It is also taken care about leap years and such |
| 130 |
% odd things. So if you enter your birthday you get your exact age in years. |
| 131 |
% You can even get the age as number with up to 18 places after the decimal |
| 132 |
% separator but I heard that this is a bit uncommon for the age of a person~\dots |
| 133 |
% |
| 134 |
% \section{Usage} |
| 135 |
% Just load the package placing |
| 136 |
% \begin{quote} |
| 137 |
% |\usepackage{calcage}| |
| 138 |
% \end{quote} |
| 139 |
% in the preamble of your \LaTeXe{} source file. |
| 140 |
% |
| 141 |
% You can also give optional package options to change the default behavior. |
| 142 |
% But you can not give values (see \nameref{sec:bugs}). So |
| 143 |
% \begin{quote} |
| 144 |
% |\usepackage[presision]{calcage}| |
| 145 |
% \end{quote} |
| 146 |
% works and sets the presision to 3. But \enquote{presision=3} is going to |
| 147 |
% fail~\dots |
| 148 |
% \bigskip |
| 149 |
% |
| 150 |
% \DescribeMacro{\calcage} |
| 151 |
% The macro |\calcage| \oarg{optional parameters} \marg{year} \marg{month} |
| 152 |
% \marg{day} takes a date and typesets the difference to the current date in |
| 153 |
% years. |
| 154 |
% \bigskip |
| 155 |
% |
| 156 |
% There are some optional parameters. \DescribePara{year, month, day} For |
| 157 |
% example you can adjust the current date for \PrintPackage{calcage} with the |
| 158 |
% keys \enquote{\PrintOptionF{year}}, \enquote{\PrintOptionF{month}} and |
| 159 |
% \enquote{\PrintOptionF{day}}. You don't have to specifier all of them. A |
| 160 |
% subset is also possible. |
| 161 |
% |
| 162 |
% Another useful parameter is \enquote{\PrintOptionF{precision}}. |
| 163 |
% \DescribePara{precision} With this parameter you can specify how many places |
| 164 |
% after the decimal separator you would like to get. The default precision is 0 |
| 165 |
% which means that |\calcage| will typeset the age in years as integer. If you |
| 166 |
% omit a value for \enquote{precision} then the precision will be 3. |
| 167 |
% |
| 168 |
% The macro |\calcage| can also take a date which is in the future. |
| 169 |
% \DescribePara{positive} The default behavior in this case is a negative |
| 170 |
% number but you can make the number positive with the |
| 171 |
% \enquote{\PrintOptionF{positive}} boolean switch. |
| 172 |
% |
| 173 |
% The next parameter I would like to show you is the \DescribePara{printyear} |
| 174 |
% \enquote{\PrintOptionF{printyear}} boolean switch. With this switch you can |
| 175 |
% specify if |\calcage| should add \enquote{year} or rather \enquote{years} |
| 176 |
% after the age. This is the default setting. |
| 177 |
% |
| 178 |
% |
| 179 |
% For the previous parameter you may need the possibility |
| 180 |
% \DescribePara{yearsuffix} (at least in the German language) to add a single |
| 181 |
% character to the plural word. For example |
| 182 |
% \begin{quote} |
| 183 |
% |Albert Einstein wurde vor \calcage[yearsuffix]{1879}{03}{14}| \\ |
| 184 |
% |geboren.| \\ |
| 185 |
% Albert Einstein wurde vor \calcage[printyear=false]{1879}{03}{14} Jahren geboren. |
| 186 |
% ^^A Ups you got my. I didn't implement a switch language feautre (yet). |
| 187 |
% \end{quote} |
| 188 |
% To do this you can use the boolean switch |
| 189 |
% \enquote{\PrintOptionF{yearsuffix}}. By default this switch is false. |
| 190 |
% If this boolean is true and the German language was selected then a |
| 191 |
% \enquote{n} will be added after \enquote{Jahre}. If the English language was |
| 192 |
% selected then this switch will not change the output. |
| 193 |
% |
| 194 |
% And last but not least there is the boolean switch \enquote{numberstring} |
| 195 |
% \DescribePara{numberstring} which is true by default. This means that the |
| 196 |
% typesetting process of an integer is done by the package |
| 197 |
% \PrintPackage{fnumprint} which will typeset the word of a number instead of |
| 198 |
% the Arabic number if the value of the number is between 0 and 12. If you |
| 199 |
% don't like this behavior you can set the boolean to false with |
| 200 |
% \enquote{numberstring=false}. |
| 201 |
% |
| 202 |
% \section{Bugs}\label{sec:bugs} |
| 203 |
% I tried to implement the possibility to change the default behavior as |
| 204 |
% package option in a nice way but with the current implementation there is a |
| 205 |
% problem with the values. If you give a key with value then the |\setkeys| |
| 206 |
% macro which gets these parameters as macro fails. I already tried |
| 207 |
% |\expandafter| so if anyone knows where the problem is or has a solution feel |
| 208 |
% free to contact me. |
| 209 |
% \bigskip |
| 210 |
% |
| 211 |
% I think there is a little inaccuracy in the conversion from days to years |
| 212 |
% because my implementation just removes the leap years before dividing. But in |
| 213 |
% my tests this seems to be more theoretically. |
| 214 |
% |
| 215 |
% The problem only has an effect if leap years are involved because if a leap |
| 216 |
% year is calculated then the additional day is subtracted. This means that one |
| 217 |
% day is not longer $\frac{1}{366}$ year but $\frac{1}{365}$ year~\dots |
| 218 |
% |
| 219 |
% \section{Examples} |
| 220 |
% \begin{verbatim} |
| 221 |
% \TeX{} Live realeases: \\ |
| 222 |
% \calcage{2012}{07}{08} \\ |
| 223 |
% \calcage{2011}{07}{20} \\ |
| 224 |
% \calcage{2010}{09}{10} \\ |
| 225 |
% \calcage{2009}{11}{09} |
| 226 |
% \end{verbatim}\vspace{-1.5em} |
| 227 |
% \TeX{} Live realeases: \\ |
| 228 |
% \calcage{2012}{07}{08} \\ |
| 229 |
% \calcage{2011}{07}{20} \\ |
| 230 |
% \calcage{2010}{09}{10} \\ |
| 231 |
% \calcage{2009}{11}{09} |
| 232 |
% |
| 233 |
% \begin{verbatim} |
| 234 |
% Donald Knuth is \calcage{1938}{01}{10} old. \\ |
| 235 |
% Donald Knuth will be 100 years old in |
| 236 |
% \calcage[positive, precision]{2038}{01}{10}. \\ |
| 237 |
% Albert Einstein died at the age of |
| 238 |
% \calcage[year=1955, month=04, day=18]{1879}{03}{14}. \\ |
| 239 |
% Age of Linus Torvalds in years: \calcage[printyear=false]{1969}{12}{28} \\ |
| 240 |
% \end{verbatim}\vspace{-1.5em} |
| 241 |
% Donald Knuth is \calcage{1938}{01}{10} old. \\ |
| 242 |
% Donald Knuth will be 100 years old in |
| 243 |
% \calcage[positive, precision]{2038}{01}{10}. \\ |
| 244 |
% Albert Einstein died at the age of |
| 245 |
% \calcage[year=1955, month=04, day=18]{1879}{03}{14}. \\ |
| 246 |
% Age of Linus Torvalds in years: \calcage[printyear=false]{1969}{12}{28} \\ |
| 247 |
% |
| 248 |
% \StopEventually{} |
| 249 |
% |
| 250 |
% \newpage |
| 251 |
% \section{Implementation} |
| 252 |
% \iffalse |
| 253 |
%<*package> |
| 254 |
% \fi |
| 255 |
% This package depends on these packages. |
| 256 |
% \begin{macrocode} |
| 257 |
\RequirePackage{fnumprint}[2012/08/27] |
| 258 |
\RequirePackage{ |
| 259 |
datenumber, |
| 260 |
fp, |
| 261 |
calc, |
| 262 |
xkeyval, |
| 263 |
kvoptions, |
| 264 |
xifthen, |
| 265 |
} |
| 266 |
% \end{macrocode} |
| 267 |
% \subsection{Declaring the options} |
| 268 |
% \begin{macrocode} |
| 269 |
\DeclareStringOption{year} |
| 270 |
\DeclareStringOption{month} |
| 271 |
\DeclareStringOption{day} |
| 272 |
\DeclareStringOption{precision}[3] |
| 273 |
\DeclareBoolOption{positive} |
| 274 |
\DeclareBoolOption{printyear} |
| 275 |
\DeclareBoolOption{yearsuffix} |
| 276 |
\DeclareBoolOption{numberstring} |
| 277 |
% \end{macrocode} |
| 278 |
% To test if all parameters are valid the macro |\ProcessLocalKeyvalOptions*| |
| 279 |
% is expanded to ensure this before leaving the preamble. This is the only |
| 280 |
% purpose for the |\ProcessLocalKeyvalOptions*| macro in this case. |
| 281 |
% \begin{macrocode} |
| 282 |
\ProcessLocalKeyvalOptions* |
| 283 |
% \end{macrocode} |
| 284 |
% The next source code line creates a new macro which captures the parameters |
| 285 |
% to use them as default options for the macro |\calcage|. |
| 286 |
% If this macro is set to \enquote{precision=4} for example the |\setkeys| is |
| 287 |
% going to fail. |
| 288 |
% \begin{macrocode} |
| 289 |
\edef\calcage@options{\@ptionlist{\@currname.\@currext}} |
| 290 |
%% ^^A \renewcommand{\calcage@options}{precision=4,printyear=false} |
| 291 |
%% ^^A Package xkeyval Error: `precision=4' undefined in families `calcage'. |
| 292 |
% \end{macrocode} |
| 293 |
% \subsection{Language selection} |
| 294 |
% Here comes the language selection part. The counter is already set by the |
| 295 |
% \PrintPackage{fnumprint} so I use it's value also in this package because I |
| 296 |
% can rely on this counter.\footnote{I am also the maintainer of the |
| 297 |
% \PrintPackage{fnumprint} package~\dots} |
| 298 |
% If you prefer another language than English or German you can redefine the |
| 299 |
% following macro definitions. |
| 300 |
% \begin{macrocode} |
| 301 |
\ifcase\value{fnumprint@language}\or |
| 302 |
% \end{macrocode} |
| 303 |
% If fnumprint@language is equal 1 |
| 304 |
% \begin{macrocode} |
| 305 |
\newcommand{\calcage@yearWord}{Jahr} |
| 306 |
\newcommand{\calcage@yearPluralSuffix}{e} |
| 307 |
\newcommand{\calcage@yearSuffix}{n} |
| 308 |
\or |
| 309 |
% \end{macrocode} |
| 310 |
% If fnumprint@language is equal 2 |
| 311 |
% \begin{macrocode} |
| 312 |
\newcommand{\calcage@yearWord}{year} |
| 313 |
\newcommand{\calcage@yearPluralSuffix}{s} |
| 314 |
\newcommand{\calcage@yearSuffix}{} |
| 315 |
\fi |
| 316 |
% \end{macrocode} |
| 317 |
% \subsection{Macro definition} |
| 318 |
% Some necessary \LaTeX{} counters are declared here before creating the |
| 319 |
% |\calcage| macro. |
| 320 |
% \begin{macrocode} |
| 321 |
\newcounter{calcage@today}\newcounter{calcage@ageindays} |
| 322 |
\newcounter{calcage@myyear}\newcounter{calcage@leapyears} |
| 323 |
% \end{macrocode} |
| 324 |
% \begin{macro}{\calcage} |
| 325 |
% And now comes the important part -- the definition of |\calcage|. |
| 326 |
% The first thing that has to be done is expanding the |\setkeys| macro. It |
| 327 |
% sets the default options then the package options get the opportunity to |
| 328 |
% overwrite these settings and finally the macro options get the same |
| 329 |
% possibility. |
| 330 |
% \begin{macrocode} |
| 331 |
\newcommand{\calcage}[4][]{% |
| 332 |
\setkeys{calcage}{precision=0, positive=true, printyear=true, |
| 333 |
yearsuffix=false, numberstring=true, |
| 334 |
year=\the\year, month=\the\month, day=\the\day, \calcage@options, #1}% |
| 335 |
\setmydatenumber{calcage@today}{\calcage@year}{\calcage@month}{\calcage@day}% |
| 336 |
\setmydatenumber{calcage@ageindays}{#2}{#3}{#4}% |
| 337 |
% \end{macrocode} |
| 338 |
% Now comes the tricky part which are the leap years~\dots \\ |
| 339 |
% My first implementation worked with dividing by $365.2425$ but this was not |
| 340 |
% perfect. So the current implementation counts how many leap years are between |
| 341 |
% the birth year and the current year and subtracts (or adds\footnote{If the |
| 342 |
% birth year is in the future because in this case the calcage@ageindays |
| 343 |
% counter will be negative.}) this number. |
| 344 |
% \begin{macrocode} |
| 345 |
\setcounter{calcage@myyear}{#2}% |
| 346 |
\setcounter{calcage@leapyears}{0}% |
| 347 |
\ifthenelse{\equal{#2}{\calcage@year}}{}{% |
| 348 |
\ifthenelse{\value{calcage@myyear}<\calcage@year}{% |
| 349 |
% \end{macrocode} |
| 350 |
% If the birth year is in the past. |
| 351 |
% \begin{macrocode} |
| 352 |
\loop% |
| 353 |
\stepcounter{calcage@myyear}% |
| 354 |
\ifleapyear{\thecalcage@myyear}\stepcounter{calcage@leapyears}\fi% |
| 355 |
\ifnum\value{calcage@myyear}<\calcage@year% |
| 356 |
\repeat% |
| 357 |
}{% |
| 358 |
% \end{macrocode} |
| 359 |
% If the birth year is in the future. |
| 360 |
% \begin{macrocode} |
| 361 |
\loop% |
| 362 |
\ifleapyear{\thecalcage@myyear}\addtocounter{calcage@leapyears}{-1}\fi% |
| 363 |
\addtocounter{calcage@myyear}{-1}% |
| 364 |
\ifnum\value{calcage@myyear}>\calcage@year% |
| 365 |
\repeat% |
| 366 |
}% |
| 367 |
}% |
| 368 |
\setcounter{calcage@ageindays}{\value{calcage@today} |
| 369 |
- \value{calcage@ageindays} - \value{calcage@leapyears}}% |
| 370 |
\ifthenelse{\boolean{calcage@positive} \AND \value{calcage@ageindays} < 0}{% |
| 371 |
\setcounter{calcage@ageindays}{\value{calcage@ageindays} * -1}% |
| 372 |
}{}% |
| 373 |
% \end{macrocode} |
| 374 |
% Because we know that every year has now exactly 365 days we can divide by |
| 375 |
% that. |
| 376 |
% \begin{macrocode} |
| 377 |
\FPdiv\calcage@age{\thecalcage@ageindays}{365}% |
| 378 |
% \end{macrocode} |
| 379 |
% The next step is to truncated the age. There is no rounding used for this. I |
| 380 |
% implemented it this way because if the age would be rounded then there would |
| 381 |
% be a wrong result in cases like $x.999$ which would become $x+1$ if |
| 382 |
% \enquote{precision=0} and that is probably not what you want~\dots |
| 383 |
% \begin{macrocode} |
| 384 |
\FPtrunc\calcage@age{\calcage@age}{\calcage@precision}% |
| 385 |
% \end{macrocode} |
| 386 |
% The last thing to do is to typeset the age which is implemented by the next |
| 387 |
% few lines of code. |
| 388 |
% \begin{macrocode} |
| 389 |
\ifthenelse{\boolean{calcage@numberstring} |
| 390 |
\AND \equal{\calcage@precision}{0}}% |
| 391 |
{\fnumprint[ein]{\calcage@age}}{\numprint{\calcage@age}}% |
| 392 |
\ifthenelse{\boolean{calcage@printyear}}{% |
| 393 |
~\calcage@yearWord% |
| 394 |
\ifthenelse{\equal{\calcage@age}{1} \OR \equal{\calcage@age}{-1}}{}{% |
| 395 |
\calcage@yearPluralSuffix% |
| 396 |
\ifthenelse{\boolean{calcage@yearsuffix}}{\calcage@yearSuffix}{}% |
| 397 |
}% |
| 398 |
}{}% |
| 399 |
} |
| 400 |
% \end{macrocode} |
| 401 |
% \end{macro} |
| 402 |
% That's it. |
| 403 |
% \begin{macrocode} |
| 404 |
\endinput |
| 405 |
% \end{macrocode} |
| 406 |
% |
| 407 |
% \iffalse |
| 408 |
%</package> |
| 409 |
% \fi |
| 410 |
% |
| 411 |
% \Finale |
| 412 |
\endinput |