On Mon, 19 May 2008 03:18:21 -0700 (PDT), New Teacher
Post by New TeacherHello,
I was a LaTex user in the early 90's when I was in grad school, but
since then I've been stuck in a MS Word world like most everyone
else. Recently, however, I've stumbled on a PC version of LaTex
(MiKTex 2.7) and I'd like to be able to use it productively for my
job.
I'm currently a high school physics teacher and this is the kind of
thing I'd like to be able to do, if possible: I need to re-create
the same physics problem with multiple sets of numerical data. For
example, I'd like to be able to set variables for, say mass and
acceleration, and possibly have them evaluated automatically and the
results inserted into the text.
m = (2)
a = (2)
A mass of (m) kg is accelerating at (a) m/s^2. What net force is
acting upon the mass?
F= ma = (2 kg)(2 m/s^2) = 4N
A mass of 2 kg is accelerating at 2 m/s^2. What net force is acting
upon the mass?
F= ma = (2 kg)(2 m/s^2) = 4N % Appears only in instructor edition %
Is it possible to do something like this?
Various people have suggested various packages that will do something
like this for you, some of which are free and some of which are not.
People have suggested using Perl to just generate the LaTeX.
I don't know how much experience with or enthusiasm for
programming you have - you could also use Python to do
this sort of thing (Python and Perl are both freely available).
Either language would work just fine - a lot of people would
say that Python code is much simpler to understand.
(A lot of Perl fans might disagree.)
What does "easily" mean? It's 3:16 and I'm about to start
writing a little Python code that will do exactly what
you ask for above - let's see how long it takes. In n
minutes I'm going to have something that you could
actually cut&paste from this post and _use_ for a variety
of problems...
OK, it's 3:33. Worked the first time (after fixing three
typos).
Why program it yourself instead of using some existin
package? (i) If the existing package doesn't do exactly
what you want you may have a hard time doing what
you want, while if it's your own code you can make it
do exactly what you want just by writing code to do
whatever (ii) once you know a little bit about a
simple language like Python you'll find you can
use it to solve all sorts of other similar problems
very easily.
Why Python? Because the syntax makes so much sense,
it's easy to learn. I wrote the code below more or less
as fast as I could type, and it worked exactly right
the first time. Ok, when you're starting you won't be
find it quite that easy until you have a little experience
with the language.
Ok, here's the code:
#For this morning pay no attention to the first 20
#or so lines - it's what defines the machine to do the
#job you asked about. You could simply use exactly
#this code as long as you wanted to do exactly
#what you ask for above - when you want to do
#something slightly different you'd modify these
#first 20 lines. For this morning skip down 20
#lines to the part that illustrates how you'd _use_
#this problem-printing machine
class Problem:
def __init__(self, vars, calculation, ST, IT):
"""ST and IT for "StudentTemplate and InstructorTemplate;
some text that defines what LaTeX you want generated for the
problem"""
self.vars = vars
self.calculation = calculation
self.ST = ST
self.IT = IT
def Format(self):
self.calculation(vars)
print 'Student text:'
print
print ST % vars
print
print 'Instrructor Text:'
print
print IT % vars
#Ok, start reading here:
#the code above defines a Problem class. You can
#use the Problem class to define various problems,
#and then print the LaTeX code for the problem.
#You have to set "vars" to a "dict" defining the
#variables, you set "calculate" to a procedure
#that does the calculation to get the answer,
#you set ST and IT to bits of LaTeX containing
#some magic code, you push the button
#by calling the Format() method and the
#LaTeX you want gets "printed". You could easily
#modify this to write the code to a file, whatever...
#For example, with the problem you post, you
#could say this:
ST = """A mass of $%(m)s$ kg is accelerating at $%(a)s$ m/s^2.
What net force is acting upon the mass?"""
IT = ST + """
F = ma = ($%(m)s$ kg)($%(a)s$ m/s^2) = $%(F)s$N"""
#Now ST and IT define how you want the problem
#to appear. Note that ST and IT are just LaTeX
#with magic additions of the form
# %(variable name)s
#wherever that magic bit appears the printout will
#have the value of the variable instead.
#Note you're going to _set_ the values of m and a
#and then the code is going to calculate the value of
#F for you:
#define the calculation that gives the answer:
def calculateFMA(vars):
vars['F'] = vars['m'] * vars['a']
#in other words, F = m*a.
#Now set up the original variables:
vars = {'m': 2, 'a': 2}
#now create the Problem:
problem = Problem(vars, calculateFMA, ST, IT)
#and now print the result:
problem.Format()
#Excellent. Here's the output of that problem.Format()
#command:
"""Student text:
A mass of $2$ kg is accelerating at $2$ m/s^2.
What net force is acting upon the mass?
Instrructor Text:
A mass of $2$ kg is accelerating at $2$ m/s^2.
What net force is acting upon the mass?
F = ma = ($2$ kg)($2$ m/s^2) = $4$N"""
#Exactly what you asked for.
#Now to do the very same problem except
#with m = 3 and a = 4 we simply modify
#vars:
vars['m'] = 3
vars['a'] = 4
problem.Format()
#Here's the output of the second problem.Format():
"""Student text:
A mass of $3$ kg is accelerating at $4$ m/s^2.
What net force is acting upon the mass?
Instrructor Text:
A mass of $3$ kg is accelerating at $4$ m/s^2.
What net force is acting upon the mass?
F = ma = ($3$ kg)($4$ m/s^2) = $12$N"""
#And just as a silly example of another problem,
#here's how you'd do the same problem in another
#universe where F = ma^2. Define a different
#calculation:
def calculateFMA2(vars):
vars['F'] = vars['m'] * (vars['a']**2)
#You also want to revise the formula appearing
#in the instructor's manual:
IT = ST + """
F = ma^2 = ($%(m)s$ kg)($%(a)s$ m/s^2)^2 = $%(F)s$[whatever units]"""
#and then
problem = Problem(vars, calculateFMA2, ST, IT)
problem.Format()
#that last produces this:
"""Student text:
A mass of $3$ kg is accelerating at $4$ m/s^2.
What net force is acting upon the mass?
Instrructor Text:
A mass of $3$ kg is accelerating at $4$ m/s^2.
What net force is acting upon the mass?
F = ma^2 = ($3$ kg)($4$ m/s^2)^2 = $48$[whatever units]"""
Post by New TeacherThank you very much
John
David C. Ullrich