alcrene / expand-latex-macros

A helper for flattening and removing macros from .tex files.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

This package combines the functionality of two other Python packages:

  • FLaP, for flattening a hierarchy of LaTeX files.
  • de-macro, for replacing macros in a document by their definitions.

It is essentially a Python 3 port of the de-macro script; FLaP is simply called before launching the de-macroing routines.


Quick-start instructions

  1. Place all your custom macro definitions in a file ending with -private.sty. For example, definitions-private.sty. <br> If you already have a .tex file you use for your personal macros, just change the extension to .sty. Then change how you import the file as described in step 2.

  2. Import your private definitions file into your .tex with \usepackage. For example:

    % In file `main.tex`
    \usepackage{definitions-private}

    Keep the definitions file simple. In particular, don't use options when loading it (e.g. \usepackage[smile]{definitions-private}).

  3. Run this script on the tex file:

```bash
expand_latex_macros main.tex outputdir
```

Just like when compiling with LaTeX, your `*-private.sty` file must be
findable from the directory where the command is *executed*.

Provided commands

This package provides the following command-line functions. Type [function name] --help for usage instructions.

  • expand-latex-macros: Replace personal macros by their definition.
  • convert-to-cmyk: Convert figures to CMYK color space.
  • rename-figures: Rename figures to the journal specifications.

To tie everything into a single automated procedure, have a look at the provided example script.

Limitations

  • Commands with default arguments are not supported. They cannot even be present in your definitions file, even if you don't use them. Probably best to avoid any kind of special logic.

  • Only put recognized commands recognized in your definitions file, specifically:

    • newcommand
    • renewcommand
    • newenvironment
    • renewenvironment

    In particular, name placeholder macros of the style \def\arnold/{Arnold Schwarzenegger} (arguably a canonical idiom) are not supported.

  • The definitions file is removed from the merged document, so any definitions left therein are lost.

Other considerations

Removing comments

To remove comments automatically, you can use the arxiv_latex_cleaner script, before launching expand-latex-macros.


Copyright 2020 Alexandre René

This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.

You should have received a copy of the GNU General Public License along with this program. If not, see https://www.gnu.org/licenses/.


Original de-macro readme

Original copyright notice:
Copyright 2005 Péter Gács
Licensed under the Academic Free Licence version 2.1

                      DE-MACRO

Version 1.3 - this version is much more conservative about deleting comments and inserting or deleting blank space: tries to leave in all comments, adds space only when necessary, and tries not to delete space in the main text. The motivating comments came from Daniel Webb. Version 1.2 - a syntactical bug corrected, thanks Brian de Alwis!

PURPOSE

This program can eliminate most private macros from a LaTeX file. Applications:

  • your publisher has difficulty dealing with many private macros
  • you cooperate with colleagues who do not understand your macros
  • preprocessing before a system like latex2html, which is somewhat unpredictable with private macros.

PLATFORM

This is a Python program, which I only have tried to run under Unix. But the Unix dependence is minimal (for example all the directory path references are platform-independent). It should be easy to adapt the program to Windows, and also to avoid command-line arguments.

In case your Python is not in /usr/bin, you should change the top line (the "shebang" line) of the program accordingly. This top line uses the -O option for python (stands for "optimize"). Without it, the program may run too slowly. If you do not care for speed, a number of other complications (the database, the checking for newer versions) could be eliminated.

USAGE

Command line:

de-macro [--defs <defs-db>] <tex-file-1>[.tex] [<tex-file-2>[.tex] ...]

Simplest example: de-macro testament

(As you see, the <> is used only in the notation of this documentation, you do should not type it.)

If <tex-file-i> contains a command \usepackage{<defs-file>-private} then the file <defs-file>-private.sty will be read, and its macros will be replaced in <tex-file-i> with their definitions. The result is in <tex-file-i>-clean.tex.

Only newcommand, renewcommand, newenvironment, and renewenvironment are understood (it does not matter, whether you write new or renew). These can be nested but do not be too clever, since I do not guarantee the same expansion order as in TeX.

FILES

<tex-file-1>.db <tex-file>-clean.tex <defs-file>-private.sty

For speed, a macro database file called <defs-file>.db is created. If such a file exists already then it is used. If <defs-file>-private.sty is older than <tex-file-1>.db then it will not be used.

It is possible to specify another database filename via --defs <defs-db>. Then <defs-db>.db will be used.

(Warning: with some Python versions and/or Unix platforms, the database file name conventions may be different from what is said here.)

For each <tex-file-i>, a file <tex-file-i>-clean.tex will be produced. If <tex-file-i>-clean.tex is newer than <tex-file-i>.tex then it stays.

INPUT COMMAND

If a tex file contains a command \input{<tex-file-j>} or \input <tex-file-j> then <tex-file-j>.tex is processed recursively, and <tex-file-j>-clean.tex will be inserted into the final output. For speed, if <tex-file-j>-clean.tex is newer than <tex-file-j>.tex then <tex-file-j>.tex will not be reprocessed.

The dependency checking is not sophisticated, so if you rewrite some macros then remove all *-clean.tex files!

About

A helper for flattening and removing macros from .tex files.

License:GNU General Public License v3.0


Languages

Language:Python 55.0%Language:TeX 41.3%Language:Shell 3.7%