Skip to content

Instantly share code, notes, and snippets.

@moble
Last active September 4, 2020 16:44
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save moble/465ae0ea31301354fa2ec4be29ea3907 to your computer and use it in GitHub Desktop.
Save moble/465ae0ea31301354fa2ec4be29ea3907 to your computer and use it in GitHub Desktop.
Get latex to tell you its sizes, then use those sizes in matplotlib to create plots that will fit perfectly in your latex file
% Anywhere you want to see the dimensions latex is using, just place
% this command in the tex file itself, run latex, and look for these
% items in the compilation output. For example, to see these numbers
% as latex uses them within the text, just put this command in the
% main text. To see them in a figure caption, just put this command
% where you write the caption text. Note that the standard
% points-per-inch ratio is 72.27.
\makeatletter
\newcommand{\ShowDimensions}{%
\typeout{The linewidth is \the\linewidth} %
\typeout{The textwidth is \the\textwidth} %
\typeout{The font encoding is \f@encoding} %
\typeout{The font family is \f@family} %
\typeout{The font series is \f@series} %
\typeout{The font shape is \f@shape} %
\typeout{The font size is \f@size} %
\typeout{The baselineskip is \f@baselineskip} %
% \typeout{The math font size is \tf@size} %
% \typeout{The math script size is \sf@size} %
% \typeout{The math scriptscript size is \ssf@size} %
\typeout{The slant is \the\fontdimen1\font} %
\typeout{The inter word space is \the\fontdimen2\font} %
\typeout{The inter word stretch is \the\fontdimen3\font} %
\typeout{The inter word shrink is \the\fontdimen4\font} %
\typeout{The extra space is \the\fontdimen7\font} %
\typeout{The xspace skip is \the\xspaceskip} %
\typeout{The hyphenation character is \the\hyphenchar\font} %
}
\makeatother
# If you want to create a plot for a full-width figure (spanning both columns of the tex output),
# use the 'tex' style *followed by* this style. This style just overrides the default figure size.
# See tex.mplstyle for more details.
figure.figsize : 7.056870, 5.645496 # Default to exact revtex text width and aspect ratio 0.8
# Use this file in your python code by placing this file in the working
# directory or the directory described in
# https://matplotlib.org/3.3.1/tutorials/introductory/customizing.html#defining-your-own-style
# Then, in your code do something like this:
#
# import matplotlib as mpl
# import matplotlib.pyplot as plt
#
# with plt.style.context(('tex')):
# plt.plot([0, 1], [2, 3])
# plt.savefig('fancy_plot.pdf')
#
# The resulting plot will be created using these values. You can use multiple
# styles with a line like this:
#
# with plt.style.context(('tex', 'tex-fullwidth')):
#
# The second style overwrites anything in the first style. You can also make
# individual adjustments to the style using something like this:
#
# with plt.style.context(('tex')):
# with plt.rc_context(rc={'axes.titlesize': 'small'}):
#
# To include the resulting plot back in your tex file, just use
#
# \begin{figure}
# \includegraphics{fancy_plot}
# \caption{\label{fig:fancy_plot}Description goes here.}
# \end{figure}
#
# Or, if you need a full-width plot, include it with
#
# \begin{figure*}
# \includegraphics[width=\linewidth]{fancy_plot_wide}
# \caption{\label{fig:fancy_plot_wide}Description goes here.}
# \end{figure*}
#
# without any extra width, scaling, or crop directives. Also note that you can
# automatically create multiple plots with something like this:
#
# for plot_style in [('tex', 'tex-fullwidth')), ('presentation')]:
# with plt.style.context(plot_style):
# plt.plot([0, 1], [2, 3])
# if plot_style[0] == 'presentation':
# plt.savefig('fancy_plot_presentation.pdf')
# else:
# plt.savefig('fancy_plot.pdf')
#
#
# The values in this file are appropriate for revtex. To adjust them for
# different styles, place the \ShowDimensions definition found in the
# accompanying show_dimensions.tex file in your tex file preamble, and then
# place `\ShowDimensions` in the part of the text where you want to know the
# dimensions — for example, in the text portion of a figure caption. When you
# compile, the log will have lines starting with "The linewidth is", showing
# you the relevant sizes. Use those numbers to set these numbers — for
# example, the font size, family, etc. Note that matplotlib wants the figure
# size in inches, so you'll need the standard conversion factor of 72.27 points
# per inch.
figure.figsize : 3.4039, 2.7 # Default to exact revtex column width (first argument), and aspect ratio 0.8
font.size : 9.0
font.family : serif
font.serif : STIX2Text, STIXGeneral, TeX Gyre Termes, DejaVu Serif, Bitstream Vera Serif, Computer Modern Roman, New Century Schoolbook, Century Schoolbook L, Utopia, ITC Bookman, Bookman, Nimbus Roman No9 L, Times New Roman, Times, Palatino, Charter, serif
font.sans-serif : STIXGeneral, TeX Gyre Termes, DejaVu Sans, Bitstream Vera Sans, Computer Modern Sans Serif, Lucida Grande, Verdana, Geneva, Lucid, Arial, Helvetica, Avant Garde, sans-serif
font.cursive : STIXGeneral, TeX Gyre Termes, Apple Chancery, Textile, Zapf Chancery, Sand, Script MT, Felipa, cursive
font.monospace : STIXGeneral, TeX Gyre Termes, DejaVu Sans Mono, Bitstream Vera Sans Mono, Computer Modern Typewriter, Andale Mono, Nimbus Mono L, Courier New, Courier, Fixed, Terminal, monospace
mathtext.fontset : stix
text.usetex : False
text.hinting : either
axes.formatter.use_mathtext : True
figure.dpi : 200 # Make these very small plots appear larger in the notebook; should not affect PDF output
figure.autolayout : False
figure.constrained_layout.use : True
savefig.pad_inches : 0
savefig.format : pdf
savefig.bbox : tight
hist.bins : auto
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment