https://github.com/d-krupke/flachtex

A simple Python-library to flatten LaTeX

https://github.com/d-krupke/flachtex

Science Score: 26.0%

This score indicates how likely this project is to be science-related based on various indicators:

  • CITATION.cff file
  • codemeta.json file
    Found codemeta.json file
  • .zenodo.json file
    Found .zenodo.json file
  • DOI references
  • Academic publication links
  • Committers with academic emails
  • Institutional organization owner
  • JOSS paper metadata
  • Scientific vocabulary similarity
    Low similarity (13.8%) to scientific vocabulary

Keywords

latex
Last synced: 11 months ago · JSON representation

Repository

A simple Python-library to flatten LaTeX

Basic Info
  • Host: GitHub
  • Owner: d-krupke
  • License: mit
  • Language: Python
  • Default Branch: main
  • Homepage:
  • Size: 154 KB
Statistics
  • Stars: 7
  • Watchers: 2
  • Forks: 1
  • Open Issues: 3
  • Releases: 6
Topics
latex
Created over 4 years ago · Last pushed about 1 year ago
Metadata Files
Readme License

README.md

flachtex

Tools (e.g. cktex, YaLafi, TeXtidote) for analyzing LaTeX-documents often only work on single files, making them tedious to use for complex documents. The purpose of flachtex is to preprocess even complicated LaTeX-documents such that they can be easily analyzed as a single document. The important part is that it also provides a data structure to reverse that process and get the origin of a specific part (allowing to trace issues back to their source). While there are other tools to flatten LaTeX, they all are neither capable of dealing with complex imports nor do they allow you to trace back to the origins.

Notable features of flachtex are:

  • Flattening of LaTeX-documents with various rules (\include, \input, \subimport ,%%FLACHTEX-EXPLICIT-IMPORT[path/to/file]...).
  • Any character in the output can be traced back to its origin.
  • Remove comments.
  • Remove \todo{...}.
  • Remove highlights of \usepackage{changes}. (This substitution is actually more robust than the one supplied with the package.)
  • Substitute commands defined by \newcommand.
  • A modular design that allows to add additional rules.

Installation

flachtex is available via pip: pip install flachtex.

Example

Let us look on a quick example that shows the power of the tool. We have a LaTeX-document consisting of three files.

main.tex

```tex \documentclass{article} \usepackage[utf8]{inputenc} \usepackage{amsmath,amssymb,amsfonts,amsthm} \usepackage{todonotes} \usepackage{xspace}

\newcommand{\importantterm}{\emph{ImportantTerm}\xspace}

%%FLACHTEX-SKIP-START Technicalities (e.g., configuration of Journal-template) that we want to skip. %%FLACHTEX-SKIP-STOP

\begin{document}

\section{Introduction}

\todo[inline]{This TODO will not be shown because we don't want to analyze it.}

Let us use \importantterm here.

% including parta with 'input' and without extension \input{./parta}

% including partb with 'include' and with extension \include{./partb.tex}

\end{document} ```

parta.tex_

```tex \subsection{Part A}

This is Part A. We can also use \importantterm here. ```

partb.tex_

tex \subsection{Part B} And Part B.

flachtex can create the following output for us that is much easier to analyze.

```tex \documentclass{article} \usepackage[utf8]{inputenc} \usepackage{amsmath,amssymb,amsfonts,amsthm} \usepackage{todonotes} \usepackage{xspace}

\newcommand{\importantterm}{\emph{ImportantTerm}\xspace}

\begin{document}

\section{Introduction}

Let us use \emph{ImportantTerm}\xspace here.

\subsection{Part A}

This is Part A. We can also use \emph{ImportantTerm}\xspace here.

\subsection{Part B} And Part B.

\end{document} ```

(currently, flachtex will actually add some redundant empty lines, but those usually do no harm and could be easily eliminated by some simple postprocessing.)

Usage

CLI

flachtex comes with a simple CLI, if you don't want to use it via Python.

``` usage: flachtex [-h] [--to_json] [--comments] [--attach] [--changes] [--changes_prefix] [--todos] [--newcommand] path

flachtex: Traceable LaTeX flattening.

positional arguments: path Path to main.tex

options: -h, --help show this help message and exit --tojson Return a json. --comments Remove comments. --attach Attach sources to json. --changes Replace the commands of the changes package. --changesprefix Use the prefix option in changes. --todos Remove todo-notes. --newcommand Automatically substitute custom commands. ```

Python

```python from flachtex import Preprocessor, remove_comments from flachtex.rules import TodonotesRule

basic usage

preprocessor = Preprocessor("/path/to/latexdocument/") preprocessor.skiprules.append(TodonotesRule()) # remove todos doc = preprocessor.expand_file("main.tex")

remove the comments (optional)

doc = remove_comments(doc)

The document can be read as a string (but contains also further information)

print(f"The process LaTeX-document is {doc}")

Get the used files

for f, data in preprocessor.structure.items(): print( f"Used file {f} which contains the content '{data['content']}' and includes" f" the files {data['includes']}." )

query origin

originfile, pos = doc.getoriginofline(line=3, col=6) print( f"The seventh character of the fourth line origins from file {originfile}:{pos}." ) originfile, pos = doc.getorigin(5) print(f"The sixth character origins from file {originfile}:{pos}.") ```

Features

Flatten LaTeX-documents

Currently, flachtex supports file inclusions of the following form:

``` % native includes/inputs \include{path/file.tex} \input{path/file.tex}

% subimport \subimport{path}{file} \subimport*{path}{file}

% manual import %%FLACHTEX-EXPLICIT-IMPORT[path/to/file] %%FLACHTEX-SKIP-START Complex import logic that cannot be parsed by flachtex. %%FLACHTEX-SKIP-STOP ```

Path Resolution

flachtex will first try to resolve the inclusion relative to the calling file. If no file is found (also trying with additional ".tex"), it tries the document folder (cwd) and the folder of the root tex-file. Afterwards, it tries the parent directories.

If this is not sufficient, try to use the %%FLACHTEX-EXPLICIT-IMPORT[path/file.tex] option.

Extending the tool

flachtex has a modular structure that allows it to receive additional rules or replace existing ones. You can find the current rules in ./flachtex/rules.

It is important that the matches do not overlap for SkipRules and ImportRules. For efficiency, flachtex will first find the matches and only then includes the files. Overlapping matches would need a complex resolution and my result in unexpected output. (It would not be too difficult to add some simple resolution rules instead of simply throwing an exception).

Usage for cleaning 'changes' of '\usepackage{changes}'

The changes-package is helpful for highlighting the changes, which is a good practice, e.g., when writing journal papers (which usually have to go through one or two reviewing iterations). These can of course disturb automatic language checkers and they have to be removed in the end. The script that is attached to the original package unfortunately is not compatible with some usages (e.g., comments can lead it astray). flachtex is capable of removing the highlights done with changes in a robust way. There are some nasty ways to trick it, but if you use brackets, it should work fine and independent of escaped symbols, comments, or line breaks.

Substitution of \newcommand

It is reasonably common to create your own commands with `\newcommand', e.g., for some terms which you may want to change later. If you want to analyze the tex-document, this can become cumbersome. Thus, flachtex gives you the option to automatically substitute such commands.

The primary reason I added this functionality to this tool (and not some higher level tool) is that I also saw that some people define their own \input/\include commands, which could not be imported easily without this feature.

Changelog

  • 0.4.0 Support for the comments package.
  • 0.3.15 Fixes Issue #8
  • 0.3.14 Bugfix by Nutron2112
  • 0.3.13 improves robustness of command parsing (of potentially faulty LaTeX code)
  • 0.3.12 Made parsing of non utf-8 encodings more robust. Some templates you get have very strange file encodings. You don't always convert them manually to utf-8.
  • 0.3.11 newcommand should work reliably with multiple arguments now (hopefully).
  • 0.3.10 Support for newcommand* substitution
  • 0.3.9: PEP compliance which may have created problems in environments without setuptools
  • 0.3.8: Substituting newcommands is no longer enabled by default.
  • 0.3.7: Versions got slightly mixed up. Should be fixed now.
  • 0.3.6 bugfix: Using findall instead of finditer.
  • 0.3.4 Dealing with \xspace in command substitution.
  • 0.3.3
    • FileFinder now has a default and allows to set a new root.
    • Command substitution for commands without parameters made more accurate.
    • from_json for TraceableString

This tool is still work in progress.

Owner

  • Name: Dominik Krupke
  • Login: d-krupke
  • Kind: user
  • Location: Germany
  • Company: TU Braunschweig

Postdoc at TU Braunschweig, IBR, Algorithms Group.

GitHub Events

Total
  • Create event: 2
  • Release event: 1
  • Issues event: 1
  • Watch event: 1
  • Issue comment event: 11
  • Push event: 8
  • Pull request event: 3
Last Year
  • Create event: 2
  • Release event: 1
  • Issues event: 1
  • Watch event: 1
  • Issue comment event: 11
  • Push event: 8
  • Pull request event: 3

Committers

Last synced: over 1 year ago

All Time
  • Total Commits: 48
  • Total Committers: 1
  • Avg Commits per committer: 48.0
  • Development Distribution Score (DDS): 0.0
Past Year
  • Commits: 0
  • Committers: 0
  • Avg Commits per committer: 0.0
  • Development Distribution Score (DDS): 0.0
Top Committers
Name Email Commits
Dominik Krupke d****e 48

Issues and Pull Requests

Last synced: 11 months ago

All Time
  • Total issues: 6
  • Total pull requests: 8
  • Average time to close issues: 3 days
  • Average time to close pull requests: 2 days
  • Total issue authors: 4
  • Total pull request authors: 2
  • Average comments per issue: 2.5
  • Average comments per pull request: 0.5
  • Merged pull requests: 7
  • Bot issues: 0
  • Bot pull requests: 0
Past Year
  • Issues: 3
  • Pull requests: 4
  • Average time to close issues: 6 days
  • Average time to close pull requests: 5 days
  • Issue authors: 2
  • Pull request authors: 1
  • Average comments per issue: 3.0
  • Average comments per pull request: 1.0
  • Merged pull requests: 3
  • Bot issues: 0
  • Bot pull requests: 0
Top Authors
Issue Authors
  • d-krupke (2)
  • caolele (2)
  • TexLayer (1)
  • b-v (1)
Pull Request Authors
  • Nutron2112 (4)
  • d-krupke (4)
Top Labels
Issue Labels
enhancement (2) bug (1)
Pull Request Labels

Dependencies

.github/workflows/publish-to-pypi.yml actions
  • actions/checkout master composite
  • actions/setup-python v1 composite
  • pypa/gh-action-pypi-publish master composite
.github/workflows/pytest.yml actions
  • actions/checkout v3 composite
  • actions/setup-python v4 composite
pyproject.toml pypi
setup.py pypi