Skip to content

Instantly share code, notes, and snippets.

@kylebutts
Created April 25, 2021 19:32
Show Gist options
  • Select an option

  • Save kylebutts/97d5f86b6f4e89c4f02b8707dd449589 to your computer and use it in GitHub Desktop.

Select an option

Save kylebutts/97d5f86b6f4e89c4f02b8707dd449589 to your computer and use it in GitHub Desktop.
How to convert .tex tables/figures/equations to .png
\documentclass[preview,convert={density=300,outext=.png}, border={4pt, 8pt}]{standalone}
% Document ---------------------------------------------------------------------
\begin{document}
$\displaystyle
\hat{\beta}=\frac{\sum_{\ell}e_{\ell}z_{\ell}y_{\ell}^{\perp}}{\sum_{\ell}e_{\ell}z_{\ell}x_{\ell}^{\perp}}
$
\end{document}
\documentclass[preview,convert={density=300,outext=.png}, border={4pt, 8pt}]{standalone}
% Document ---------------------------------------------------------------------
\begin{document}
\begin{equation}
\hat{\beta}=\frac{\sum_{\ell}e_{\ell}z_{\ell}y_{\ell}^{\perp}}{\sum_{\ell}e_{\ell}z_{\ell}x_{\ell}^{\perp}}
\end{equation}
\end{document}
\documentclass[convert={density=300,outext=.png}, border={8pt, 16pt}]{standalone}
% AMS --------------------------------------------------------------------------
\usepackage{amsmath}
\usepackage{amsfonts}
\usepackage{amsthm}
\usepackage{graphicx}
% Font -------------------------------------------------------------------------
\usepackage[T1]{fontenc}
\usepackage[utopia, varg]{newtxmath}
\renewcommand{\rmdefault}{futs} % Utopia as text font
% Small adjustments to text kerning
\usepackage{microtype}
% Color Palette ----------------------------------------------------------------
\usepackage{xcolor}
% https://www.materialpalette.com/colors
\definecolor{navyblue}{HTML}{0A3044}
% Table and Figure labelling ---------------------------------------------------
\usepackage{caption}
% multifigure with \caption
% \begin{subfigure} \end{subfigure}
\usepackage{subcaption}
\DeclareCaptionLabelSeparator{threedash}{\,---\,}
\DeclareCaptionFont{navyblue}{\color{navyblue}}
\captionsetup[table]{format=plain, labelsep=threedash, font={navyblue, bf}}
\captionsetup[figure]{format=plain, labelsep=threedash, font={navyblue, bf}}
% Tables -----------------------------------------------------------------------
% Table with easy to use footnotes
\usepackage[flushleft]{threeparttable}
\setlength\labelsep{0pt}
% \toprule, \cmidrule, \bottomrule
\usepackage{booktabs}
% Slighty more spacing between rows
\usepackage{array}
\renewcommand\arraystretch{1.1}
\begin{document}
\begin{threeparttable}
\caption{Bias from Misspecification}
\begin{tabular}{@{} l *{6}{r} @{}}
% Head
\toprule
& \multicolumn{6}{c}{Data-Generating Process} \\
\cmidrule{2-7}
& \multicolumn{1}{r}{\textbf{Within 40mi}} & \multicolumn{1}{r}{\textbf{Within 80mi}} & \multicolumn{1}{r}{\textbf{Within 40mi.}} & \multicolumn{1}{r}{\textbf{Within 80mi.}} & \multicolumn{1}{r}{\textbf{Decay}} & \multicolumn{1}{r}{\textbf{Decay}} \\
Specification & & & \multicolumn{1}{r}{\textbf{(Additive)}} & \multicolumn{1}{r}{\textbf{(Additive)}} & & \multicolumn{1}{r}{\textbf{(Additive)}} \\
% Body
\midrule
TWFE (No Spillovers) & $0.258$ & $0.258$ & $0.258$ & $0.258$ & $0.258$ & $0.258$ \\
Within 40mi. & $-0.005$ & $0.213$ & $-0.005$ & $0.176$ & $0.159$ & $0.143$ \\
Within 80mi. & $-0.009$ & $-0.009$ & $-0.009$ & $-0.009$ & $-0.009$ & $-0.009$ \\
Within 100mi. & $-0.006$ & $-0.006$ & $-0.006$ & $-0.006$ & $-0.006$ & $-0.006$ \\
Within 40mi. (Additive) & $0.043$ & $0.221$ & $-0.006$ & $0.177$ & $0.174$ & $0.143$ \\
Within 80mi. (Additive) & $0.034$ & $0.134$ & $-0.012$ & $-0.009$ & $0.099$ & $-0.010$ \\
Decay & $-0.159$ & $0.070$ & $-0.174$ & $0.014$ & $-0.009$ & $-0.033$ \\
Decay (Additive) & $-0.023$ & $0.148$ & $-0.084$ & $0.019$ & $0.088$ & $-0.008$ \\
Rings (0-20, 20-30, 30-40) & $-0.005$ & $0.213$ & $-0.005$ & $0.176$ & $0.159$ & $0.143$ \\
Rings (0-20, 20-30, 30-40, 40-60, 60-80) & $-0.009$ & $-0.009$ & $-0.009$ & $-0.009$ & $-0.009$ & $-0.009$ \\
Rings (0-20, 20-30, 30-40, 40-60, 60-80) (Additive) & $0.036$ & $0.134$ & $-0.008$ & $-0.008$ & $0.100$ & $-0.009$ \\
\bottomrule
\end{tabular}
\end{threeparttable}
\end{document}

The standalone class in LaTex allows to convert individual equations, tables, and figures into .png files. It does require ImageMagick to convert pdf to image and crop

Important Note: If you are using a \begin{table}, \begin{figure}, or \begin{equation}/$$ environments, you must add the preview option to the \documentclass command or else it will not compile. For tables, I just use the \begin{tabular}\end{tabular} environment instead and it works fine and I just use $\displaystyle$ for equations.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment