1

I came across other tex questions where people asked ways to represent numbers in comma-separated format. That is writing 1234567 as 1,234,567. I need this while teaching addition/subtraction to my child. I am trying to generate randomized list of addition problems which gets harder by day. I basically want to run the code and generate say 30-days worth of daily problem sets which gets harder as we go. While I can tweak it for my own child on the go, there are other children involved and they will not have the chance of accessing me. So, it is on the parents. Long story short, here is the challenge I do have:

The previously given solutions to this problem have two issues:

  1. Many do not work well enough in math mode!

  2. (and this is my main issue) I would like to do something like this (using numprint package as a failing example): $\numprint{\sage{12454785*34}}$

As you can guess, this does not work as \numprint reads a string and it does not bode well with \sage{...} within! I thought of defining my own sage function and then using \sagestr{} but it provides a temporary solution as I will later use the same set of files to generate some online content which does NOT bode well with \sagestr (another story!).

  • 2
    Welcome to tex.sx! Could you please provide a minimal working example that shows the problem or what you have already tried? – siracusa Jul 3 '17 at 5:35
  • Did you come across this answer to the query How to create multipage tables of binary arithmetical problems (and associated answers) involving pseudo-random numbers? Adapting that answer to pretty-print large numbers (by using commas as thousands-separators) should be a piece of cake. – Mico Jul 3 '17 at 7:23
  • Please provide more information about your desired document setup. E.g., please state explicitly which document class should be used, the page and textblock dimensions, the number of math questions per page, and what you mean by "degree of difficulty". Separately, how crucial is that that thousands-separators be provided for numbers with more than 3 (or 4?) digits be provided? – Mico Jul 3 '17 at 7:45
1

Just some thoughts rather than an answer:

  • Personally I would avoid sage. LaTeX can do calculations on its own (at least with expl3).
  • You should not reinvent the wheel. Your \numprint would, in any case, be inferior to siunitx's \num.
  • Update: As Mico mentioned in the comments below your question you could consider adapting one of the solutions from this question. In combination with my following code snippet, egreg's version would fit.

Here's a very simple approach of a \numberfromcalc macro which just expects some numeric expression to calculate and then prints the formatted result regardless whether you are in math mode or not.

\documentclass{article}

\usepackage{siunitx}

\ExplSyntaxOn
\NewDocumentCommand { \numberfromcalc } { m }
    {
        \int_set:Nn \l_tmpa_int { #1 }
        \num[group-separator={,}]{\int_use:N \l_tmpa_int}
    }
\ExplSyntaxOff

\begin{document}
    \noindent\numberfromcalc{12454785*34}\\
    $\numberfromcalc{12454785*34}$
\end{document}

numbers

As you did not post some code I won't try to generate some random lists here, but that would not be that hard either. As I said, this is just a starter.

0

One of the advantages of using Sage is it's built on Python. So you get lots of built in functionality without lengthy, hard to read code. In this case, change the numbers to strings in Sage and insert the commas before going to LaTeX.

\documentclass{article}
\usepackage{sagetex}
\begin{document}
\begin{sagesilent}
a = Integer(randint(1000,10000))
b = Integer(randint(1000,10000))
\end{sagesilent}
\noindent What's $\sagestr{"{0:12,.0f}".format(a)} \times \sagestr{"{0:12,.0f}".format(b)}$?\\
Answer: $\sagestr{"{0:12,.0f}".format(a*b)}$
\end{document}

The output running in CoCalc looks like this: enter image description here

Information on the command to create the commas is here.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.