Friday, December 28, 2012

Quizzes in $\rm{\LaTeX}$: Shuffling questions and answers

A problem when one wants to write a quiz with multiple-choice answers in $\rm{\LaTeX}$ is how to shuffle questions and their possible answers. One typically wants to produce different versions of the quiz, with the questions and their possible answers appearing in different random order from version to version. This is easy to do with some placeholders in the tex file and with the use of Python and a little bash scripting. Although there is probably an easier way to achieve what I describe below, this is the way I came up with and I like it!

First one has to take some care in preparing the quiz. As always, the tex file will be split in three parts:
  1. The preamble,
  2. The main body (which will include the problems),
  3. The ending.
In the preamble one typically starts with $\verb1\documentclass{}1$ and ends with $\verb1\begin{document}1$. Here, we enclose all that, and everything we want to print in the top of every version, in between a $\verb1%BeginPreamble1 \ldots \verb1%EndPreamble1$. In the main body we enclose each problem in a $\verb1%BeginProblem1 \ldots \verb1%EndProblem1$, and we follow the formatting in the example tex file you can download by following the link at the end of this post. Finally, we include the ending enclosed in $\verb1%BeginEnding1 \ldots \verb1%EndEnding1$.

Now, in order to produce the different versions, I wrote a bash script and a Python program. The script calls the Python program that does all the shuffling, and then it compiles the produced tex files (i.e. the different versions) and cleans up. The result is four versions of tex and the corresponding pdf files. Of course one can produce more or fewer versions by modifying the scripts. It is important that all the formatting of the example file is followed, otherwise the scripts will not run properly. By the way you will need Python 3 to run my scripts. (Python 2 will do too, but the bash script "shuffle" has to be modified by deleting the "3" from "python3".)

At the end of the each of the tex files, after the $\verb1\end{document}1$, the key is printed for convenience. Two pdf's are produced for each version: one with the correct answers indicated with a red checkmark, and one with no correct answers indicated. By the way, the correct answer is identified by typing $\verb1\answer1$ at the end of an item (see example file). I should also point out here that if you want a specific possible answer to not be included in the shuffling, you can make that happen with the flag $\verb1%dontshuffle1$. This is useful, for example, when one of the possible answers is "None of the above". Again, the example file in the link below includes all the relevant formatting that has to be followed.

So all one has to do after downloading the zip file below is unzip it, and run the shuffle script by opening a terminal and typing $\verb1./shuffle1$. It is important that only one tex file is in the folder of the files "shuffle" and "", but its name can be anything (it is read automatically by the script).

Download example by clicking here!

Tuesday, July 12, 2011

MathJax in Blogger (II)

The new awesome way to include math in a website is MathJax. Until recently, one had to install MathJax in their own server in order to include math in their webpages. That changed a few weeks ago; you can now specify in your html code that the required script be loaded from MathJax's website.

To get MathJax to work in Blogger, just go to your Blogger account, click "Design" (top right of the page), and then "Edit HTML". After the first <head> you see, paste
<script src='' type='text/javascript'>    
        HTML: ["input/TeX","output/HTML-CSS"],
        TeX:extensions: ["AMSmath.js","AMSsymbols.js"], 
               equationNumbers: { autoNumber: "AMS" } },
        extensions: ["tex2jax.js"],
        jax: ["input/TeX","output/HTML-CSS"],
        tex2jax: { inlineMath: [ ['$','$'], ["\\(","\\)"] ],
                   displayMath: [ ['$$','$$'], ["\\[","\\]"] ],
                   processEscapes: true },
        "HTML-CSS": { availableFonts: ["TeX"],
                      linebreaks: { automatic: true } }

You're done! You can now use the usual $...$ or \(...\) for inline equations, and $$...$$ or
\[...\] for equations that are centered in their own line.

Time for an example:
\cos^2\theta-\sin^2\theta=\cos 2\theta.
\] The above script loads AMSmath and AMSsymbols so that the macros defined in these popular packages are available for use in your blog.

UPDATE: Equations can now be numbered and referred to. For example, in Newtonian gravity we have
\end{equation}and so equations (\ref{eq:gravt}) and (\ref{eq:gravo}) lead to\[
\]assuming the equivalence of inertial and gravitational mass.

Tuesday, February 15, 2011

We're lucky $(1+x/n)^n$ converges!

If you're ever interested in borrowing money from a bank, here are some calculations that can help you make the right choice.

Suppose you want to borrow an amount of money $m$, at a yearly interest rate of $x\%$ for a $y$ number of years. The first thing you'd wonder is how much your monthly payment will be. Well, to calculate that, you have to know exactly how the bankers have done the math. As expected, they have tried to maximize the interest you pay to them.

Here's how it goes. The yearly interest rate is not actually applied every year, as any reasonable human being would expect, but is divided by 365 and applied every day. The reason for that will be clear below, but let's assume for now, in order to make the math easier, that the interest is applied every year.

At the beginning of the first year you borrow $m$. At the end of the first year you owe \[m-12p,\] where $p$ is the monthly payment. At this point the interest is applied and at the beginning of the second year you owe \[m_2=\left(1+\frac{x}{100}\right)(m-12p).\]At the end of the second year you owe $m_2-12p$, and at the beginning of the third year you owe \[m_3=\left(1+\frac{x}{100}\right)\left[\left(1+\frac{x}{100}\right)(m-12p)-12p\right],\] which simplifies to \[m_3=\left(1+\frac{x}{100}\right)^2 m-12\left[1+\left(1+\frac{x}{100}\right)\right]p.\] From this form of $m_3$ and after a moment's thought (which, by the way, might take more than a moment), it becomes clear that at the beginning of the $n$-th year you owe \[m_n=\left(1+\frac{x}{100}\right)^{n-1}m-12\sum_{i=1}^n \left(1+\frac{x}{100}\right)^{i-1}p.\] The sum can be recognized as the sum of the first $n$ terms of a geometric sequence with ratio $1+x/100$, and is equal to \[\sum_{i=1}^n \left(1+\frac{x}{100}\right)^{i-1}=n\left(1+\frac{x}{100}\right)^{n-1}.\] Since at the end of the loan term you can't owe anything, you set \[m_y=0,\] from which you can finally calculate the monthly payment, \[p=\frac{m}{12}\frac{\frac{x}{100}\left(1+\frac{x}{100}\right)^{y-2}}{\left(1+\frac{x}{100}\right)^y-1}.\] If you followed the math so far that's good. It should be obvious, though, that if a banker did the math that way, then they would be out of business very quickly, for that way they actually allow you to pay off capital without them having charged any interest for it. So it sort of makes sense that they use slightly more sophisticated math, but as I'll show they take it to the opposite extreme.

As I already mentioned the calculation of the monthly payment is actually done with the interest divided by the number of days in a year and applied every day. The equation from which the monthly payment follows is \[\left(1+\frac{x}{100d}\right)^{yd-1}m-\frac{12}{d}\sum_{i=1}^{yd} \left(1+\frac{x}{100d}\right)^{i-1}p=0,\] where $d$ is the number of days in a year, which, to account for leap years, you can take to be 365.2422. It is important to realize that the bank increases the frequency with which they apply the (divided by the frequency) interest, which brings them more money. This is just because of the behavior of the sequence \[a_n(z)=\left(1+\frac{z}{n}\right)^n;\]the higher the $n$ the bigger the $a_{n}(z)$. However, $a_n(z)$ converges to $e^z$, and so the bankers can't just take the frequency to infinity and have as pay as much interest as they want. The title of the post should now make sense; however, although we should be happy that $(1+z/n)^n$ converges, we can still be upset that it doesn't converge fast enough.

Note that the monthly payment we calculated in the previous paragraph was calculated as if they did allow you to pay every day, i.e. as if there was really an appropriately calculated daily payment that gave the monthly payment. That would be a fair system. However, the bankers are not fair; they actually apply the interest every day, but they only allow you to pay every month instead of every day. Needless to say, this produces a monthly payment higher than the one in the previous scenarios we considered. Think about this a little bit; they keep applying the (appropriately divided) interest every day, and they don't allow you to intervene and do something about it until the end of the month (some banks allow biweekly payments). By the way, the equation that gives $p$ with the current system is actually (I assume that a month has $365.2422/12=30.43685$ days) \[\left(1+\frac{x}{100d}\right)^{yd-1}m-\sum_{i=1}^{12y}\left(1+\frac{x}{100d}\right)^{30.43685(i-1)}p=0.\]

Tuesday, February 8, 2011

MathJax in Blogger

I recently came across MathJax, the amazing new open-source approach to displaying $\rm{\LaTeX}$ on the web. Finally we can use fonts for $\rm{\TeX}$ on the web, instead of these ridiculously ugly figures that substitute the $\rm{\TeX}$ code, and whose size and alignment induces vomiting.

One can install MathJax on their server and use it to include beautifully-typeset math in their webpages. To get it to work in Blogger, follow the steps below.
  1. ssh into your server and install MathJax as described here (preferably with the svn method so that you can easily update MathJax in the future).
  2. Since (most likely) Blogger is not hosted on your server, follow the instructions here. You basically need to create the file .htaccess in the server's mathjax/fonts/HTML-CSS/TeX/otf folder and paste the following text in it:
    <FilesMatch "\.(ttf|otf|eot)$">
    <IfModule mod_headers.c>
    Header set Access-Control-Allow-Origin "*"
  3. Finally, go to your Blogger account, click "Design", and then "Edit HTML". After the first <head> you see, paste
    <script type="text/javascript" src="your-server/mathjax/MathJax.js">
            extensions: ["tex2jax.js","TeX/AmsMath.js","TeX/AMSsymbols.js"],
            jax: ["input/TeX","output/HTML-CSS"],
            tex2jax: {
                inlineMath: [ ['$','$'], ["\\(","\\)"] ],
                displayMath: [ ['$$','$$'], ["\\[","\\]"] ],
                processEscapes: true,
            "HTML-CSS": { availableFonts: ["TeX"] }
    where your-server is to be substituted with your server's http address.
You're done! You can now use the usual $...$ or \(...\) for inline equations, and $$...$$ or
\[...\] for equations that are centered in their own line.

Time for an example:\[\sin^2\theta+\cos^2\theta=1.\]Note that the code above loads AMSmath.js and AMSsymbols.js, so that the macros and environments they provide are defined for use on your blog.

Sunday, May 30, 2010

Representations and particles

The idea of a group representation gives flesh and bones to the abstract notion of a group. A set of objects satisfying the group axioms has no tangible meaning, and that is why we need to represent those objects in a way that allows us to work with them.

Consider a Lie group $G$ and a vector space $V$. One can construct the group of general linear transformations on $V$, $GL(V)$. A representation $V$ of $G$ is a Lie group homomorphism, from $G$ to $GL(V)$:\[\rho: G\to GL(V)\] (Note the abuse of terminology here: we call $V$ the representation, not $\rho$.)

In physics textbooks one often reads that some field $\phi$ furnishes a representation of the Lorentz group (which is a Lie group). The point is that we know how the Lorentz group acts on spacetime (on $(t,\mathbf{x})$), i.e. we know one representation of the Lorentz group, and we want to find how it acts on functions on spacetime, i.e. on $\phi(t,\mathbf{x})\equiv \phi(x)$. Of course these functions are the fields of quantum field theory. (Note that we will not consider internal symmetries like $SU(3)_\text{color}$ here, but the discussion extends trivially to those.)

It turns out that the Lorentz group acts on scalar functions (corresponding to scalar fields like the Higgs field), spinorial functions (corresponding to spinor fields like the electron field), vectorial functions (corresponding to gauge fields like the photon), and the list goes on. The quantum number that distinguishes all these representations is spin. Note that the spaces where these fields live are representations of the Lorentz group, and that is where the terminology we mentioned in the beginning of the previous paragraph comes from.

The important point is that the representations of the Lorentz group correspond to fields which, after quantization, correspond to the particles we observe in the real world.