GeneralNumber Output inside Info for new Class fails

LaTeX specific issues not fitting into one of the other forums of this category.
Post Reply
EmilioLazo
Posts: 15
Joined: Sat Jul 16, 2011 1:59 am

Number Output inside Info for new Class fails

Post by EmilioLazo »

Hi!

I have a macro named \Total which holds a number that is the result of some math operations with package fp-upn from fp. I want to show the output of \numprint{\Total} from numprint in a \ClassWarning or \ClassInfo to be shown in the log. (I'm making a class).

If I put \ClassInfo{factura}{\numprint{\Total}} inside the class, the log file reads:

Code: Select all

Class factura Info: ** Total -> Bs.\numprint 1462.50000000000000000
The number is what is held in \Total but this isn't in the format after applying \numprint because \numprint doesn't work inside \ClassInfo.

What trick can be used to show inside \ClassInfo the result of \numprint{\Total}?

I did try:

Code: Select all

\let\TOTAL\numprint\Total
\ClassInfo{factura}{\numprint{\TOTAL}}
also I did try with \expandafter and \protect.

There is a way to hold inside another macro the result of evaluating \numprint{\Total}? This way the macro holds the number and not the operation with \numprint that cannot be evaluated inside \ClassInfo.

Thanks in advance!

Recommended reading 2024:

LaTeXguide.org • LaTeX-Cookbook.net • TikZ.org
LaTeX Beginner's Guide LaTeX Cookbook LaTeX TikZ graphics TikZによるLaTeXグラフィックス
User avatar
cgnieder
Site Moderator
Posts: 2000
Joined: Sat Apr 16, 2011 7:27 pm

Number Output inside Info for new Class fails

Post by cgnieder »

The \ClassInfo command (like every command that writes something to a file) expands its argument. However, \numprint from the numprint package is not expandable, unfortunately. I don't quite understand what your aim is but you need to find another (expandable) way.

Regards
site moderator & package author
EmilioLazo
Posts: 15
Joined: Sat Jul 16, 2011 1:59 am

Number Output inside Info for new Class fails

Post by EmilioLazo »

Hi Clemens,

For example:

Code: Select all

\documentclass{article}

\usepackage{numprint}
\npthousandsep{.}
\newcommand{\Total}{1234}

\begin{document}

\noindent Total: \Total \\
Total with numprint: \numprint{\Total}

\ClassInfo{article}{The total is: \numprint{\Total}}

\end{document}
Log file reads:

Code: Select all

Class article Info: The total is: \numprint {1234} on input line 12.
Instead of:

Code: Select all

Class article Info: The total is: 1.234 on input line 12.
When you say that \numprint is not expandable means that what I want is not possible with \numprint?

Thanks!
User avatar
cgnieder
Site Moderator
Posts: 2000
Joined: Sat Apr 16, 2011 7:27 pm

Number Output inside Info for new Class fails

Post by cgnieder »

EmilioLazo wrote:When you say that \numprint is not expandable means that what I want is not possible with \numprint?
Exactly. I don't understand why you want \numprint in \ClassInfo in the first place but you have to find another way.

Regards
site moderator & package author
EmilioLazo
Posts: 15
Joined: Sat Jul 16, 2011 1:59 am

Number Output inside Info for new Class fails

Post by EmilioLazo »

cgnieder wrote:
EmilioLazo wrote:When you say that \numprint is not expandable means that what I want is not possible with \numprint?
Exactly. I don't understand why you want \numprint in \ClassInfo in the first place but you have to find another way.

Regards
I want \numprint's in the log file because I'll use grep to find strings, and I prefer having all these strings in the same format. Not only \Total is written to the log; there are 4 macros (4 amounts) written into the log, and after operating them with fp-upn, depending on the operation, some of them ends with floating point and some others without it; i.e. all the macros I want to have shown in the log file are expressed in different formats!

So, there is a need to standarize the way I'll express these quantities, and would be very useful finding a way similar to \numprint{\Total} but that can be called inside a \ClassInfo!

Thanks!!
Last edited by cgnieder on Fri Mar 22, 2013 11:37 pm, edited 1 time in total.
User avatar
cgnieder
Site Moderator
Posts: 2000
Joined: Sat Apr 16, 2011 7:27 pm

Number Output inside Info for new Class fails

Post by cgnieder »

If you would tell us which format you want (rounded? decimal comma instead of point?) and which different forms the input can have maybe someone can come up with something. Until now it is still rather vague...

Regards
site moderator & package author
EmilioLazo
Posts: 15
Joined: Sat Jul 16, 2011 1:59 am

Number Output inside Info for new Class fails

Post by EmilioLazo »

cgnieder wrote:If you would tell us which format you want (rounded? decimal comma instead of point?) and which different forms the input can have maybe someone can come up with something. Until now it is still rather vague...

Regards
You mean using a different way of rouding/formatting the number rather than using \numprint to be \ClassInfo-compliant? if so, what I need is:

- thousand separator: "."
- decimal separator: ","
- two decimal points rounded
User avatar
cgnieder
Site Moderator
Posts: 2000
Joined: Sat Apr 16, 2011 7:27 pm

Number Output inside Info for new Class fails

Post by cgnieder »

Ok, here's a way that meets the requirements you've specified. The main point here is that the formatted result is stored in a macro that can be expanded. I've used expl3 (the l3kernel programming layer) for convenience:

Code: Select all

\documentclass{article}
\usepackage[T1]{fontenc}
\usepackage{expl3}

% make @ a letter (this is default in a class file):
\makeatletter
% turn expl3-namespace on:
\ExplSyntaxOn
% settings:
\tl_new:N   \l_emilio_thousands_sep_tl
\tl_set:Nn  \l_emilio_thousands_sep_tl { . }
\tl_new:N   \l_emilio_decimal_sep_tl
\tl_set:Nn  \l_emilio_decimal_sep_tl { , }
\int_new:N  \l_emilio_round_places_int
\int_set:Nn \l_emilio_round_places_int { 2 }

% internal token lists:
\tl_new:N   \l_emilio_integer_part_tl
\tl_new:N   \l_emilio_decimal_part_tl

% the main command:
\cs_new_protected:Npn \emilio_format_number:n #1
  {
    \tl_set:Nx \l_tmpa_tl { \fp_eval:n {round( #1 , \l_emilio_round_places_int ) } }
    \exp_after:wN \emilio_format_number:w \l_tmpa_tl .. \q_stop
  }

% internal command that separates integer from decimal part,
% formats them and builds the final result:
\cs_new_protected:Npn \emilio_format_number:w #1.#2.#3 \q_stop
  {
    \emilio_format_integer_part:n { #1 }
    \emilio_format_decimal_part:n { #2 }
    \tl_clear:N \emilio@formatted@number
    \tl_put_right:NV \emilio@formatted@number \l_emilio_integer_part_tl
    \tl_put_right:NV \emilio@formatted@number \l_emilio_decimal_sep_tl
    \tl_put_right:NV \emilio@formatted@number \l_emilio_decimal_part_tl
  }

% format the integer part:
\cs_new_protected:Npn \emilio_format_integer_part:n #1
  {
    \tl_clear:N \l_emilio_integer_part_tl
    \tl_set:Nn \l_tmpa_tl { #1 }
    \tl_reverse:N \l_tmpa_tl
    \int_zero:N \l_tmpa_int
    \tl_map_inline:Nn \l_tmpa_tl
      {
        \int_incr:N \l_tmpa_int
        \tl_put_left:Nn \l_emilio_integer_part_tl { ##1 }
        \int_compare:nT { \int_mod:nn { \l_tmpa_int } { 3 } = 0 }
          { \tl_put_left:NV \l_emilio_integer_part_tl \l_emilio_thousands_sep_tl }
      }
  }

% format the decimal part:
\cs_new_protected:Npn \emilio_format_decimal_part:n #1
  {
    \tl_set:Nn \l_emilio_decimal_part_tl { #1 }
    \int_compare:nT { \tl_count:V \l_emilio_decimal_part_tl < \l_emilio_round_places_int }
      {
        \int_do_while:nNnn
          { \tl_count:V \l_emilio_decimal_part_tl } < { \l_emilio_round_places_int }
          { \tl_put_right:Nn \l_emilio_decimal_part_tl { 0 } }
      }
  }

% macros that can be used outside the expl3-namespace:
\tl_new:N \emilio@formatted@number
\cs_new_eq:NN \emilio@numprint \emilio_format_number:n

% turn expl3-namespace off:
\ExplSyntaxOff
% make @ other again:
\makeatother

\begin{document}

\newcommand\Total{1234}

\makeatletter
\emilio@numprint{1234567890}
\meaning\emilio@formatted@number

\emilio@numprint{\Total}
\meaning\emilio@formatted@number

\emilio@numprint{1234.567890}
\meaning\emilio@formatted@number

\emilio@numprint{12.34567890}
\meaning\emilio@formatted@number

\emilio@numprint{\Total}
\ClassInfo{article}{The total is: \emilio@formatted@number}
\makeatother

\end{document}
Regards
site moderator & package author
EmilioLazo
Posts: 15
Joined: Sat Jul 16, 2011 1:59 am

Number Output inside Info for new Class fails

Post by EmilioLazo »

cgnieder wrote:Ok, here's a way that meets the requirements you've specified. The main point here is that the formatted result is stored in a macro that can be expanded. I've used expl3 (the l3kernel programming layer) for convenience: ...
Hi Clemens!

Really thank you for your help, and your code! but I've compilation errors; the command "\emilio@numprint" is not understood ("Undefined control sequence"), and because I don't understand that LaTeX3 code, I can't debug it now! I'm sorry.

Maybe you've forgotten something or not pasted the whole code!

Can you look at this code again?

Thanks again Clemens,
Best regards.
User avatar
cgnieder
Site Moderator
Posts: 2000
Joined: Sat Apr 16, 2011 7:27 pm

Number Output inside Info for new Class fails

Post by cgnieder »

EmilioLazo wrote:Maybe you've forgotten something or not pasted the whole code!

Can you look at this code again?
It is working as expected. You can verify this if you click on the »OPEN IN WRITELATEX« link above the code box. I suspect (but this is a shot in the very dark) that your TeX distribution isn't up to date.

Regards
site moderator & package author
Post Reply