Browsed by
Category: LaTeX

Fitting tables to the width of a page

Fitting tables to the width of a page

Ever have the problem of Stata regression output being a little too wide? Worry no more.

I use Ben Jann’s excellent esttab to export Stata regressions into LaTeX documents.

My only problem with esttab is that the tables can be too wide, i.e. wider than the width of the text in the PDF. So I made a few edits to esttab that automatically scale the tables to the text-width.

I have called this program estwide. You can download it here. As it is based on estout, Ben Jann should be considered a co-author. Click here to see an example of its effect. (If you wish to replicate the above example, you can download the associated do-file here and the TeX file here.)

To use estwide:
1. Make sure estout is installed. To do this, in Stata type ssc inst estout, replace
2. Save estwide.ado to the same folder that estout is now installed in. You can check the folder by typing which estout
3. Restart Stata.
4. Rather than exporting your tables using the esttab command, simply replace esttab with estwide, e.g. estwide using hello.tex, style(tex) replace
5. Make sure you have both the adjustbox and booktabs LaTeX packages installed.
6. Make sure you have called both of these packages up by including \usepackage{booktabs} and \usepackage{adjustbox} in the header of your LaTeX file.
7. Include your tables as normal. You can copy and paste the output into your TeX file, or have the tables update automatically when you make changes by using \input{myfilename}.

Update, September 2017: after some emails from people, I have two things to add. Firstly, estwide seems to work much better if you include a caption to the table.

Secondly, if you have a problem with the caption appearing on one page and the table itself on another, wrap the input in a LaTeX table. For example, this code works well for me:

Partial derivative shortcut in LaTeX

Partial derivative shortcut in LaTeX

This is incredibly simple code but may be of use to some people. Typing out derivatives in LaTeX (e.g. \frac{\partial y}{\partial x}) can be shortened.

Add this line of code to the header of your document:


This creates a new command \dydx that takes two arguments, e.g. \dydx{P}{\tau}. Include this command in math mode (e.g. encased in dollar signs) and it will produce \frac{\partial P}{\partial \tau}.

Some help with common LaTeX problems

Some help with common LaTeX problems

What’s the easiest way to include a graphic (or graph, diagram, figure etc.) in my document?

\caption{This is my picture}

Then compile using pdflatex.

I’m getting a “no BoundingBox” error when I compile my document.

Have you tried compiling the document with pdflatex? In WinEdt, select PDFLaTeX from the dropdown menu that’s below ‘Accessories’ on the menu bar, and to the right of the recycle bin icon. If you’re compiling from the command prompt and your document is named thesis.tex, type pdflatex thesis.

I’m trying to compile my document and I am using natbib to do the references, but the document won’t compile with PDFLaTeX.

It’s unfortunate that natbib (as far as I can see) relies on latex (not pdflatex) but latex gets confused if you have graphics in the document. My workaround for this is to temporarily place a % sign in front of any graphics to turn them into comments, rather than code. This means the graphics are initially excluded, and you can run latex/bibtex and natbib will work. Once all the references are good and ready, you can remove the % signs (a quick Find > Replace All will do the trick) and compile using pdflatex.

Simple Problem Set Hack

Simple Problem Set Hack

I had a problem set where each question had multiple sub-parts: a, b, c; etc. I was typing the solution up in LaTeX and it wasn’t obvious what was the cleanest way to identify my answers.

I ended up with code like this:

\section*{Question 1}
\new This is my answer to part a.

\new This is my answer to part b

This formatting results in something like this:

That’s exactly what I was looking for. It requires two lines of code at the start of your document:

\newcommand{\new}{\vspace{0.3cm}\addtocounter{my_count}{1} (\alph{my_count}) }

LaTeX mathematical expectation shortcut

LaTeX mathematical expectation shortcut

I find it cumbersome to typeset {\mathbb E}[x], “the expectation of x”, in LaTeX. I’ve simplified the code a little.

First, include these lines in the header of your document:

\newcommand{\e}[1]{{\mathbb E}\left[ #1 \right]}

This calls on the necessary packages and makes a new function \e which takes one argument. The outcome is that argument within appropriately-sized square brackets and prefaced with the expectation sign.

For example, the code \e{x^2}=10 results in {\mathbb E}[x^2]=10.

Writing MCQs in LaTeX: the qanda class

Writing MCQs in LaTeX: the qanda class

I was writing a series of practice multi-choice questions for the class I teach when I decided I wanted to write a LaTeX function to automate the formatting. I wanted to include the answer as an argument of that function (possibly with room for an explanation) but not to display the answer until the end of the document. (This is a reasonably simple process. For a more hardcore setup, see the eqexam package.)

I hacked away at the endnotes package until I got what I wanted. Editing packages is a pain though, so I summarized the changes into a Questions and Answers class: qanda.cls.

This does the dirty work in the background and defines two new commands: \q and \qtf. The former takes six arguments: the question, four multi-choice options, and the answer. The latter takes four arguments: the question, two multi-choice options (e.g. true and false, thus the tf in command name), and the answer.

This makes writing multi-choice questions very easy. Here is some sample code.

\title{An Example of the qanda Class}
\author{Enda Hargaden}
This document is an example of the output of the qanda (``q and a'') class.

\q{Who wrote \textit{Animal Farm}?}{Hargaden}{Orwell}{Marx}{Huxley}{Orwell}
\qtf{There are subtle historical parallels in \textit{Animal Farm}.}{True}{False}{True; it is an allegory for the unfortunate route Communism took.}
\q{Who wrote \textit{Capital}?}{Hargaden}{Orwell}{Marx}{Huxley}{Marx}



This produces this nifty document.

To use the qanda class:

  1. Download qanda.cls
  2. Place it in the folder your .tex is in
  3. Start your document with \documentclass{qanda}

To produce the answers to your questions, simply include the \theendnotes command where you want them in your document. Preface this with \newpage if you want them on a new page.

Note that the qanda class is based on the scrartcl class, so you need that on your computer. You also will need to have installed the enumerate and setspace packages.


Latin phrases and Greek letters for economists

Latin phrases and Greek letters for economists

Latin phrases for economists
Phrase Translation
A priori Pre-supposed; your “priors beliefs”.
Ad infinitum To infinity; and so on.
Ad valorem Per value; a 10% tax is an ad valorem tax, whereas a duty per gallon of gasoline is not.
Ceteris paribus All else equal e.g. “We would expect, ceteris paribus, that an increase in price would lower quantity demanded.”
De facto For all practical purposes, but not officially.
De jure By law.
Ex ante Before the event; in anticipation.
Ex post After the event; in retrospect.
Per se Literally; by itself.
Prima facie In the first instance; at first glance.
QED As has been asked to be shown; done.
Ultra vires Beyond their power, e.g. the court ruled that Congress were acting ultra vires.
Greek letters for economists
Lower-case Upper-case Pronunciation Economic meaning
\alpha A Alpha Capital share of income
\beta B Beta Regression coefficient (econometrics), or rate of time discounting (economic theory)
\gamma \Gamma Gamma N/A
\delta \Delta Delta Depreciation
\epsilon E Epsilon Elasticity
\zeta Z Zeta N/A
\eta H Eta N/A
\theta \Theta Theta Type, e.g. \theta_H might represent a “high type”
\iota I Iota N/A
\kappa K Kappa N/A
\lambda \Lambda Lambda The Lagrange multiplier
\mu M Mu Mean
\nu N Nu N/A
\xi \Xi Xi N/A
o O Omicron N/A
\pi \Pi Pi Inflation, or profit (lower-case) or the product of a series (upper-case)
\rho R Rho The coefficient of autoregression
\sigma \Sigma Sigma Standard deviation (lower-case) or the sum of series (upper-case)
\tau T Tau Tax
\upsilon \Upsilon Upsilon N/A
\phi \Phi Fy N/A
\chi X Chi (rhymes with `guy’) Used in statistics
\psi \Psi Psi (like `Si’) N/A
\omega \Omega Omega N/A

Some Useful Latin and Greek (PDF)
Some Useful Latin and Greek (TeX)

A clarification on LaTeX

A clarification on LaTeX

Just in case you’ve googled me and wondered why I happily link to something called LaTeX, I should clarify that LaTeX (most commonly pronounced “Lay-tek”) is a computer language for typesetting documents.¬†(Well, it’s officially called \LaTeX and is a markup language rather than a standard computer language, but that’s splitting hairs.)

Most people use Microsoft Word to write documents. That’s fine. I prefer to use LaTeX, particularly if there is any mathematics in the document. The primary reason for this is ease. LaTeX can “read” math and I find it easier to type \sum x_i to obtain \sum x_i than to mess around with the equation editor.

LaTeX also produces remarkably beautiful documents. I appreciate that this sounds particularly nerdy, but it’s true: ask Dario for proof.¬†Moreover, its settings are defaulted to look just right, so there’s no messing around with fonts, margins, line-spacing, etc.

Andrew Roberts has a thorough outline of the benefits of LaTeX. Here’s my summary:

  1. Math(s) is much easier to write.
  2. Presentations (via beamer) are cooler.
  3. The documents look better.
  4. You don’t have to worry about how it looks, so you can focus on content.

I admit the name is unfortunate, though.