This is a partial solution and probably it's not what you want. However, I'm posting it solely for record purposes, in case anyone would need it. =)
I'll use Maxima:
Maxima is a system for the manipulation of symbolic and numerical expressions, including differentiation, integration, Taylor series, Laplace transforms, ordinary differential equations, systems of linear equations, polynomials, and sets, lists, vectors, matrices, and tensors. Maxima yields high precision numeric results by using exact fractions, arbitrary precision integers, and variable precision floating point numbers. Maxima can plot functions and data in two and three dimensions.
First of all, I created a script file named solve.cmd in my working directory:
@echo off
rem If Maxima is not in your path, you need to set it.
rem Also, I'm making use of the 'head' command, you can
rem obtain the Win32 version easily with MSys.
set PATH=%PATH%;"C:\Program Files\Maxima-5.24.0\bin"
maxima --very-quiet -r %1 | head --lines=-1
I'm not in my Linux box right now, but a .sh file is similar.
Using Jake's code, I added a call to this script:
\documentclass{article}
\usepackage{pgf}
\begin{document}
\section{Problem}
\pgfmathsetseed{1}
\pgfmathtruncatemacro\coeffa{random(2,10)}
\pgfmathtruncatemacro\coeffb{random(2,10)}
\pgfmathtruncatemacro\coeffc{random(2,10)}
% call to the 'solve' script providing the coefficients.
% Maxima's 'solve' function will return x1 and x2, and the
% 'tex' function will convert the answer to TeX. The last
% line of the output will be removed (the 'head' command)
% because Maxima adds a 'false' line to the end of the file.
% Then we redirect the output to a file.
\immediate\write18{solve.cmd "tex(solve(\coeffa*x^2 + (\coeffb*x) + (\coeffc)));" > solution1.tex}
Let \(x_1\) and \(x_2\) be the roots of \(\coeffa x^2 +\coeffb x +\coeffc=0\). Find \(x_1x_2\).
\section{Solution}
% read the solution
\input solution1
\end{document}
I tried to use Maxima's plot2d
function together with the call, but I failed to fix a minor error with file extensions. I intended to create a png
plot with gnuplot
and include it. Sorry, it didn't work yet.
Don't forget to use the --shell-escape
or --enable-write18
flag.
EDIT: It gave me a tremendous headache, but I guess I found a nice improvement. =)
I'm not used to Maxima, though I like to use it for simple calculations. If my code is somehow incorrect, please help me improve it. Thanks.
I edited the solve.cmd file in my working directory to look like this:
@echo off
rem If Maxima is not in your path, you need to set it.
rem Now, I don't use 'head' anymore, but 'grep'. The idea
rem behind this code is:
rem 1. generate the three coefficients,
rem 2. output both equation and solution to a file,
rem 3. get rid of the 'false' lines in the text (Maxima's fault)
rem (we do this thanks to 'grep' using an inverse flag)
rem 4. direct output to a file 'problem.txt'.
@echo off
set PATH=%PATH%;"C:\Program Files\Maxima-5.24.0\bin"
maxima --very-quiet -r "s: make_random_state(true)$ set_random_state(s)$ fullrand(low, high) := floor(random(1.0) * ( 1 + high - low)) + low$ a : fullrand(-10,10)$ s: b : fullrand(-10,10)$ c : fullrand(-10,10)$ tex(a*x^2 + b*x + c); tex(solve(a*x^2 + b*x + c));" | grep -v false > problem.txt
Now the tricky part, the LaTeX code:
\documentclass{article}
\immediate\write18{solve.cmd}
\newread\myread
\openin\myread=problem.txt
\read\myread to \theequation
\read\myread to \thesolution
\begin{document}
\section{Problem}
This is our equation:
\theequation
\section{Solution}
And this is our solution:
\thesolution
\end{document}
Let's check the code: first of all, we run solve.cmd
, which outputs both equation and solution to a file named problem.txt
. Then we create a \newread\myread
and open this file \openin\myread=problem.txt
. Now we map the first line to \theequation
(which is the equation which we defined in our Maxima code) and the second line to \thesolution
(same logic). Then we call it when needed.
Again, probably some of my Maxima code is faulty. But I hope you get the idea behind this. In case of changing the equation or solution to a inline version, we may probably do some sed
in it and replace the pattern. =)
What I typically do is to create a folder, say figures
, to organize all images. The folder hierarchy would be:
latex-project/
figures/
foxjumpingoverdog.jpg
...
document.tex
...
Then add the command \graphicspath{./figures/}
in the preamble of the document to point to the folder.
Using the package epstopdf
allows us to incorporate .eps
figures.
A MWE followed by the output is below:
\documentclass{article}
\usepackage[demo]{graphicx}
\graphicspath{ {./figures/} }
\usepackage{epstopdf}
\begin{document}
\noindent The quick brown fox jumps over the lazy dog. See Figure~\ref{fig:figurelbl}.
\begin{figure}[h]
\centering
\includegraphics[width=0.5\textwidth]{foxjumpingoverdog}
\caption{\label{fig:figurelbl} An illustration of the quick brown fox \emph{jumping} over the lazy dog.}
\end{figure}
\end{document}
Best Answer
The
\rand
instruction computes a random number and stores it in thesec
counter. On the other hand, the argument to\includegraphics
should be a file name (after macro expansion), not the set of instructions to compute it.Just perform the computation before doing
\includegraphics
:Note
\rand
will do some computations which are not allowed in an "expansion only" context. The string passed as argument to\includegraphics
(or\input
, that uses the same mechanism) can contain macros, but these should be able to expand to characters only.Extensions
If you need to use files numbered from 01 to 20, say, you have to ensure that the command prints a padding zero for numbers less than 10; the LaTeX kernel provides
\two@digits
for this:For more digits, you can emulate the behavior of
\two@digits
:Two digits
Three digits
Use again
\rand\includegraphics[width=0.6cm]{excl\printrandom c.png}