latexipy package¶
Module contents¶
LaTeXiPy¶
- Provides
An easy way of generating plots in multiple formats.
Sensible defaults that fit perfectly with most LaTeX documents.
Compatibility with Matplotlib-based packages.
Easy restyling.
This documentation assumes that latexipy
has been imported as lp
:
>>> import latexipy as lp
You will probably use lp.figure()
the most (once for each block):
>>> with lp.figure('filename'):
... draw_the_plot()
-
latexipy.
figure
(filename, *, directory='img', exts=['pgf', 'png'], size=None, mkdir=True)[source]¶ The primary interface for creating figures.
Any Matplotlib-derived code in the scope of this context manager is valid, and should output as expected.
- Parameters
filename (str) – The base name of the file, without extensions.
directory (Optional[str]) – The name of the directory in which to store the saved files. Default is ‘img’.
exts (Sequence) – A list of all the extensions to be saved, without the dot. Default is [‘pgf’, ‘png’].
size (Optional[Sequence[float, float]]) – The width and height of the figure, in inches. Default is
figure_size()
.mkdir (Optional[bool]) – Whether the directory should be created automatically if it does not exist. Default is True.
- Raises
FileNotFoundError – If the target directory does not exist and cannot be created.
NotADirectoryError – If the target directory is actually a file.
PermissionError – If there is no permission to write to the target directory.
ValueError – If the format is not supported.
Notes
When integrating with LaTeX, the recommended format is PGF. PNG can be used externally, such as in blog posts or as embedded images, while PDF can be standalone, or inserted into LaTeX documents. A full list of supported formats can be found by calling
plt.gcf().canvas.get_supported_filetypes_grouped()
-
latexipy.
figure_size
(width_tw=0.9, *, ratio=None, height=None, n_columns=1, max_height=8, doc_width_pt=345)[source]¶ Get the necessary figure size.
- Parameters
width_tw (Optional[float]) – The width of the figure, as a proportion of the text width, between 0 and 1. Default is 0.9.
ratio (Optional[float]) – The ratio of the figure height to figure width. If
height
is specified,ratio
is calculated from that andwidth
. Default is the golden ratio.height (Optional[float]) – The height of the figure in inches. If
ratio
is specified,height
is ignored. Default is the golden ratio of the width.n_columns (Optional[int]) – The number of equally sized columns in the document. The figure will never be larger than the width of one column. Default is 1.
max_height (Optional[float]) – The maximum height of the figure, in inches. Default is
MAX_HEIGHT_INCH
.doc_width_pt (float) – The text width of the document, in points. It can be obtained by typing
\the\textwidth
in the LaTeX document. Default is 345.
- Returns
width (float) – The figure width, in inches.
height (float) – The figure height in inches.
-
latexipy.
latexify
(params={'axes.labelsize': 8, 'axes.titlesize': 8, 'font.family': 'serif', 'font.monospace': [], 'font.sans-serif': [], 'font.serif': [], 'font.size': 8, 'legend.fontsize': 8, 'pgf.preamble': ['\\usepackage[utf8x]{inputenc}', '\\usepackage[T1]{fontenc}'], 'pgf.texsystem': 'xelatex', 'text.usetex': True, 'xtick.labelsize': 8, 'ytick.labelsize': 8}, new_backend='pgf')[source]¶ Set up Matplotlib’s RC params for LaTeX plotting.
Call this function before plotting the first figure.
- Parameters
params (Optional[dict]) – A dictionary containing the RC params that need to be updated. Default is
PARAMS
. The defaults should be okay for most cases, butPARAMS
can be updated via.update()
as well.new_backend (Optional[str|None]) – The backend to switch too. Default is PGF, which allows a nicer PDF output too.
- Raises
ValueError – If the new backend is not supported.
Example
>>> params = PARAMS.copy() >>> params.update({'font.family': 'sans-serif'}) >>> latexify(params)
-
latexipy.
save_figure
(filename, directory, exts, mkdir=True, from_context_manager=False)[source]¶ Save the figure in each of the extensions.
- Parameters
filename (str) – The base name of the file, without extensions.
directory (str) – The name of the directory in which to store the saved files.
exts (Sequence) – A list of all the extensions to be saved, without the dot.
mkdir (Optional[bool]) – Whether the directory should be created automatically if it does not exist. Default is True.
from_context_manager (Optional[bool]) – Whether the function is being called from the
figure
context manager. This only affects the logging output. Default is False.
- Raises
FileNotFoundError – If the target directory does not exist and cannot be created.
NotADirectoryError – If the target directory is actually a file.
PermissionError – If there is no permission to write to the target directory.
ValueError – If the format is not supported.
Notes
When integrating with LaTeX, the recommended format is PGF. PNG can be used externally, such as in blog posts or as embedded images, while PDF can be standalone, or inserted into LaTeX documents. A full list of supported formats can be found by calling
plt.gcf().canvas.get_supported_filetypes_grouped()
-
latexipy.
temp_params
(font_size=None, font_family=None, font_serif=None, font_sans_serif=None, font_monospace=None, params_dict=None)[source]¶ Temporarily set Matplotlib’s RC params.
- Parameters
font_size (Optional[int]) – The font size to use. It changes all the components that are normally updated with
latexify()
. If you want to change something individually, do so from withinparams_dict
.font_family (Optional[str]) – The font family to use.
font_serif (Optional[List[str]]) – A list of serif fonts to use.
font_sans_serif (Optional[List[str]]) – A list of sans-serif fonts to use.
font_monospace (Optional[List[str]]) – A list of monospace fonts to use.
params_dict (Optional[Dict[str, Any]]) – The dictionary of parameters to update, and the updated values. This is only applied after going through the rest of the arguments.