3

Overview

My title is the not the best, and I will edit it if there are any better suggestions! In a nutshell, what I am trying to achieve is to randomly change numbers in a maths question and have a solution that appropriately 'solves' the that questions.

I am also considering more than just TeX in my solution - a solution using other languages such as C++ to generate the tex file to be complied by pdflatex is fine too. The end goal would be to have an autonomous system. A simpler example of what I'm thinking of (in both terms of maths and not using LaTeX) can be found here: http://www.tutorfinder.com.au/maths/multiplication.php

Finally, I already have a complete template using exam class which I am hoping to use. If your solution requires a package that is not compatible with exam class, please let me know!

My Current Thoughts

Pure LaTeX Solution

Suppose a simple question is:

Solve $\intop 6\sin(2x)\, dx$

And my solution that I wish to show (requires mathtools package) is:

\begin{align*}
\shortintertext{\text{Recall from the formula sheet that:}}
    \intop\sin(nx)\, dx & =-\frac{1}{n}\cos(nx)+C\\
\shortintertext{\text{Here, \ensuremath{n=2}}}
    \therefore\intop6\sin(2x)\, dx & =6\times\left(-\frac{1}{2}\right)\cos(2x)+C\\
        & =-3\cos(2x)+C
\end{align*}

Is there a way to randomise the 2 in 2x such that it is between 1 and 4, and randomise the 6 in front of the sin(2x) between -2 and 10, such that it changes every time the tex file is complied?

And then , where 6\times\left(-\frac{1}{2}\right) occurs, this will be replaced by the multiplication of the new randomised numbers? I'm thinking of defining the 2 and the 6 as variables and then using one of the calculator packages to find the product. Later though, I will need LaTeX to solve more complex equations such as trigonometry (e.g. sin(0.4)), simple definite integrals (e.g. integral from 0 to 1 of x^3), permutations (e.g. nCr, nPr, factorials) and normal distributions...

This is where my other idea comes in:

Generate TeX File Using Another Program

Because I don't know what the capabilities of LaTeX for generating random numbers and making calculations, I'm considering using another language to calculate the relevant coefficients and then print it in a tex file with relevant markup. For example:

Program:
<a> = random integer between -2 and 10  (here: 6)
<b> = random integer between 1 and 4    (here: 2)
<c> = <a> / <b>     (here: 6/2 = 3)

Then, it prints:

Solve $\intop <a> \sin(<b>x)\, dx$
\begin{align*}
\shortintertext{\text{Recall from the formula sheet that:}}
    \intop\sin(nx)\, dx & =-\frac{1}{n}\cos(nx)+C\\
\shortintertext{\text{Here, \ensuremath{n=2}}}
    \therefore\intop<a>\sin(<b>x)\, dx & =<a>\times\left(-\frac{1}{<b>}\right)\cos(<b>x)+C\\
        & =-<c>\cos(<b>x)+C
\end{align*}

The benefit is when it comes to solving integrals or probabilities, I know there will be a library/tool box/add on available that can find the answer. Also, I know if statements and such will work.

For example, with parabolas there can be zero, one or two intercepts. I could code the program to determine what the discriminant is, and then print the correct using if elseif statements depending on what the sign of the discriminant. I have no idea how I would do this in LaTeX...

Summary

I know this is a long post and I do apologise. It's because my question is rather specific and I don't want some kind hearted person to spend hours writing up a solution only to find that they'd misinterpreted what I'm trying to achieve.

I'm really looking for packages in LaTeX that can do what I'm looking for and can be implemented in my question; or suggest an efficient way to create a program that does this; or some other amazing idea that is much more efficient than my current thoughts.

Cheers, Alwin

  • Maybe you should try lualatex and use lua randomizing function with the \directlua primitive. I know it is possible, but never tried to do so. – sztruks Jan 12 '16 at 15:47
  • 1
    This is related to tex.stackexchange.com/questions/20404/…. I suggested using SageTeX there, but there are several good choices. – John Palmieri Jan 12 '16 at 15:57
  • 1
    Sagetex would be my choice, too, because it farms out the math to a computer algebra system so you can easily get derivatives, graphs, and sin/cos/tan using square roots (instead of a decimal), etc. There are a lot of sample sagetex problems here. The "Handouts" page has a sample sagetex test template, the Basics page has sample worksheets with solutions, etc. – DJP Jan 12 '16 at 16:23
  • 1
    I think your main question is easy but after Later though, I will need LaTeX to solve more complex equations such as trigonometry ... it's no more clear. Please ask one clear question by (question) – touhami Jan 12 '16 at 16:24
  • may be related tex.stackexchange.com/questions/20404/… – touhami Jan 12 '16 at 16:48
2

I was looking for a similar approach recently and my question was solved by using lua code within my document Using LuaLaTeX to calculate lengths in cm to be used in TikZ drawing. In the attached code I have a question regarding Magnetism and all numbers are randomly generated, which will in turn create a new question + answers. I also included some functions for rounding and formatting scientific notation so that I can use the numbers both for siunitx display as well as further calculations. The only limitation is, that this will need to be compiled with LuaLaTeX.

\documentclass{article}

\usepackage[binary-units]{siunitx}
\usepackage{luacode}

% shorthand command to define and set a variable as a length
\newcommand*{\nvar}[2]{%
\newcommand*{#1}{}
\edef#1{#2}
}
\newcommand*{\rvar}[2]{%
\renewcommand*{#1}{}
    \edef#1{#2}
}


\begin{document}

\begin{luacode}
function round2(num, idp) -- rounding numbers to a defined number of decimal points
  return tonumber(string.format("%." .. (idp or 0) .. "f", num))
end

function roundScientific(num, idp) -- rounding numbers to a defined number of decimal points
  return string.format("%." .. (idp or 0) .. "e", num)
end


function magneticFieldStrengthH ( I,N,L, printStr ) -- function to calculate the magnetic field strength H with a given current I, number of turns N and length L
    if printStr == true then
        result= (tex.print(round2(I * N / L ,3 )) )
    else
        result = I * N / L 
    end
    return result
end

function fluxDensityB(mu, H, printStr) -- function to calculate the flux density B with a given permittivity mu and magnetic field strength H
    if printStr == true then
        result = tex.print( roundScientific(mu * H,3) )
    else
        result = mu * H
    end
    return result
end

function totalFluxPhi( B,A,printStr ) -- function to calculate the total flux Phi with a given flux density B and cross-sectional area A
    if printStr == true then
        result = tex.print( roundScientific( B * A,3) ) 
    else
        result = B * A
    end
    return result
end

-- seed lua PRNG with os time
math.randomseed( os.time() )
\end{luacode}


%\nvar{\currentI}{4}
\nvar{\currentI}{\directlua{tex.print(math.random(1,10)) }}
%\nvar{\lengthL}{0.6}
\nvar{\lengthL}{ \directlua{tex.print(math.random(1,10)/10 ) } }
%\nvar{\turnsN}{200}
\nvar{\turnsN}{\directlua{tex.print(math.random(1,10) * 100) } }
%\nvar{\crossareaA}{500e-6}
\nvar{\crossareaA}{\directlua{tex.print( (math.random(1,10) * 100) .. "e-6" )}}


    A coil of \turnsN\ turns is wound uniformly onto a wooden ring having a
 mean circumference of \SI{\lengthL}{\metre} and a uniform cross sectional area of
   \SI{\crossareaA}{\square\metre}.  If the current through the coil is 
   \SI{\currentI}{\ampere} and $\mu = 4\pi \times 10^{-7} 
  \si{\weber\per\ampere\per\metre}$, calculate:


            \begin{itemize}
                \item Magnetic field strength. $\directlua{magneticFieldStrengthH(\currentI, \turnsN, \lengthL, true) } \frac{\si{\ampere}\text{ turns}}{\si{\metre}}$
            \item Flux density. \SI{ \directlua{fluxDensityB(4e-7 * math.pi, magneticFieldStrengthH(\currentI, \turnsN, \lengthL,false),true ) } }{\weber\per\square\metre}
            \item Total flux. \SI{\directlua{ totalFluxPhi(fluxDensityB(4e-7 * math.pi, magneticFieldStrengthH(\currentI, \turnsN, \lengthL,false),false ),\crossareaA,true)  } }{\weber}
            \end{itemize}


\end{document}

EDIT

The old example MWE created new values every single time a macro was used i.e. \currentI, \lengthL, et cetera. An answer (LuaLaTeX output from tex.print not interpreted) from Joseph Wright set me on the right path, in that I could use \edef (What is the difference between \let and \edef?) to ensure that \directlua is only expanded once. I incorporated the adjustments to my code in the definitions of nvar and rvar

1

Using the links provided in the comments, I was able to figure out a way to answer my own question that didn't require an external program:

\begin{questions}

%---
%\pgfmathsetseed{1}
\pgfmathtruncatemacro\coeffa{random(1,10)}
\pgfmathtruncatemacro\coeffb{random(2,8)}
\pgfmathtruncatemacro\coeffc{\coeffa/gcd(\coeffa,\coeffb)}
\pgfmathtruncatemacro\coeffd{\coeffb/gcd(\coeffa,\coeffb)}
%---
\question[2] Solve $\displaystyle \intop \coeffa \sin(\coeffb x)\, dx$

\begin{solutionorlines}[3\lines]
\begin{align*}
\shortintertext{\text{Recall from the formula sheet that:}}
    \intop\sin(nx)\, dx & =-\frac{1}{n}\cos(nx)+C\\
\shortintertext{\text{Here, \ensuremath{n=\coeffb}}}
    \therefore\intop \coeffa \sin(\coeffb x)\, dx & = \coeffa\times\left(-\frac{1}{\coeffb}\right)\cos(\coeffb x)+C\\
    & =-\frac{\coeffc}{\coeffd}\cos(\coeffb x)+C 
\end{align*}
\end{solutionorlines}

\end{questions}

It does seem to take a while to compile, so I'll still check out other methods suggested to see if the increased speed is worth increased complexity.

Cheers, Alwin

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.