GithubHelp home page GithubHelp logo

latex.py's Introduction

latex.py is a preprocessor for LaTeX source files.

It was originally written because a journal asked me not to have any
user-defined latex macro in the source file. Which was a problem, since
I had lots of them. Hence, a program was needed to process the file and
expand these macros to create a file to send to the publisher.

Since then, it has grown a bit. There are now three main use cases:

1) Write macros in python. Because it's simpler? More powerful libraries?
   Use '@' instead of '\' as macro prefix. Typical build process:
       python latex.py -i macros.py -o manuscript.pdf manuscript.tex

2) Expand user-defined macros in text, because some journals don't like these.
   Uses '\' as macro prefix, but replaces as many as possible with their
   expansions.
       python latex.py -L -o manuscript.texp manuscript.tex

3) Parse a latex file to determine its dependencies, for use in makefiles etc.
       deps=$(python latex.py -L \
              -P includegraphics:1:%s.pdf \
              -P input:1:%s.tex \
              -P bibliography:1:%s.bib \
              manuscript.tex)

In the .tex file, lines beginning with '%@', or between '{%@' and '}%@', 
form a block of python code. As a simple case, the following can be put in 
the .tex file,

---
{%@
def vec(x):
    return r'\hat{%s}' % x
A = r'\mathrm{A}'                        [1]
}%@
---

and now \vec{\A} is expanded to \hat{\mathrm{A}} in the text (assuming 
the -L flag is used).

There are various shortcuts to make this more pleasant to write. The 
following are two equivalent ways to write the definitions above:

---
{%@
def vec(x):
    return : \hat{#(x)}                  [2]
A =: \mathrm{A}
}%@
---
{%@
vec =: \hat{%s}                          [3]
A   =: \mathrm{A}
}%@
---
\newcommand{\vec}[1]{\hat{#1}}           [4]
\newcommand{\A}{\mathrm{A}}

(The ':' syntax is not pure python obviously. For the curious, line [2] is
converted to 
    return r"""\hat{%(x)s}""" % locals()
Furthermore, a macro can either be a string (as in lines [1] and [3]), or
a function (as in [2]). Strings are formatted using the arguments to the
macro.

[4]: Yes, latex.py understands \newcommand when the -L flag is given. It
can be used on an unmodified latex file to expand all user-defined macros.


Another short example:

---
%@eval = eval                              [5]
\title{Proving that $2+2=@eval{2+2}$
---

The line [5] is required to make the builtin function eval visible as a macro. 
Note that these builtins may be shadowed by user variables, but are always 
accessible as for example __builtin__.eval.
It is recommended that functions, variables and classes that should not be
visible as macros should contain the underscore character ('_'), since it is
not allowed in a macro name.


See the examples/ directory for other examples.


latex.py was written in 2010 by Joachim B Haga ([email protected]).
It is licensed under GPL v2 (or later).

latex.py's People

Contributors

jobh avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

latex.py's Issues

Typo in the doc

In the doc you write python parse.py where you mean python latex.py, I guess you changed the name of your program at some point.

Anyhow, thanks a lot ! I've been looking for a program like this to pre-process user-defined macros and you script seems to be doing the job just fine.

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.