GithubHelp home page GithubHelp logo

nilsvu / texfig Goto Github PK

View Code? Open in Web Editor NEW
67.0 3.0 9.0 491 KB

Utility to generate PGF vector files from Python's Matplotlib plots to use in LaTeX documents.

License: MIT License

Python 95.85% TeX 4.15%
python pgf latex vector-graphics matplotlib plotting

texfig's Introduction

TexFig

DOI

Python's Matplotlib can save plots as PGF vector files, with text containing LaTeX code and all. LaTeX can render PGF files. Sounds like a perfect match, doesn't it? (It does. See my bachelor thesis for example plots.)

Example for a PGF plot in LaTeX

Why PGF?

  • It's a vector format. No blurry pixel graphics anymore!
  • Any text in your plots will be typeset with your document's native font and style.
  • You can use your custom LaTeX \newcommand macros in your plots after defining a dummy implementation for them in Python. They will be typeset correctly in your document.

Usage

  1. Download the repository and run

    python example.py

    to test your Python and LaTeX setup.

  2. Copy the texfig.py file next to your Python script. It is just some simple setup and utility code for matplotlib to get you started, so feel free to edit any configurations within.

  3. Use texfig to generate PGF plots:

    # import texfig first to configure Matplotlib's backend
    import texfig  # assuming texfig.py and an __init__.py is in the directory
    # then, import PyPlot as usual
    import matplotlib.pyplot as plt
    
    # obtain a nicely configured figure from texfig
    fig = texfig.figure()
    # plot as usual
    plt.plot(range(10))
    # save your plot as both a PGF file (for inclusion in LaTeX) and a PDF file (for preview only)
    texfig.savefig("example_plot")

    You can adjust the settings in texfig.py to your liking. In particular, you may want to set the default_width to your LaTeX \textwidth (in inches). In the pgf.preamble list you can also define any macros that you have implemented in your LaTeX document and that you wish to use in your plots. Don't worry about implementing them correctly here, since they are rendered by your LaTeX document later anyway.

    Make sure that no call to change matplotlib backends precedes the import texfig statement, and do not change backends afterwards either. In particular, refrain from using %matplotlib inline configurations in Jupyter Notebooks.

  4. Now \usepackage{pgf} and \input the PGF file in your LaTeX document:

    % in the preamble
    \usepackage{pgf}
    % somewhere in your document
    \input{example_plot.pgf}
  5. Admire the beauty of LaTeX vector plots.

Hint: If your PGF plots end up slowing down your LaTeX compile times too much, consider omitting the PGF rendering in draft mode like so:

% in the preamble
\usepackage{ifdraft}
\newcommand{\plot}[1]{\ifdraft{\includegraphics{#1.pdf}}{\input{#1.pgf}}}
% somewhere in your document
\plot{example_plot}

Now when you give your \documentclass the option draft, a placeholder of the correct size is rendered instead of the PGF plot. Note that for this to work you need to keep the preview PDF generated by texfig.savefig in addition to the corresponding PGF file in the same directory.

Contact

TexFig was created and is maintained by Nils Leif Fischer.

License

TexFig is released under the MIT license. See LICENSE.md for details.

texfig's People

Contributors

abe404 avatar nilsvu avatar ploth avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

texfig's Issues

Not working. Eror: The control sequence at the end of the top line of your error message was never \def'ed.

This project is a really good idea! I have been searching and testing for hours until I found this project. Generating a figure with the example code works, but the inclusion in latex sadly does not. Latex throws the following error multiple times:

Finished to include the file. (./test_plot.pgf
! Undefined control sequence.
<recently read> \mathdefault 
             
l.93 ...fi}\catcode`\%=\active\def%{\%}$\mathdefault
                                                  {0}$}}%
The control sequence at the end of the top line
of your error message was never \def'ed. If you have
misspelled it (e.g., `\hobx'), type `I' and the correct
spelling (e.g., `I\hbox'). Otherwise just continue,
and I'll forget about whatever was undefined.

! Undefined control sequence.
<recently read> \mathdefault 

License is missing

This project does not have any license, therefore one is not allowed to use this code, in a strict sense.

It would be nice if you could choose a license such that one could use this code and contribute to it.

If you don't care what people do with your code, then I'd recommend the MIT/Expat license or the BSD-2-Clause license.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.