TeX Output

When you are writing a paper or some other text, which uses data that is produced by a experiment, it is very useful to have direct access from your TeX sources to your experiment results. This is especially useful if you want to reproduce your paper with a different version of your programs.

class versuchung.tex.Macros(filename='data.tex')[source]

Can be used as: input parameter and output parameter

A Macros file is a normal File with the extension, that you can define TeX macros easily. This is especially useful for writing texts. You may have a experiment, which may be an analysis to an experiment that produces raw data. The produced numbers should appear in your LaTeX document. So instead of copying the numbers you can define TeX macros and use them in the text. This is especially useful if you work on the experiment and the text in parallel and the numbers change often.

>>> from versuchung.tex import Macros
>>> macro = Macros("/tmp/test.tex")
>>> macro.macro("MyNewTexMacro", 23)
>>> print macro.value
\newcommand{\MyNewTexMacro} {23}
comment(comment)[source]

Add a comment in the macro file

macro(macro, value)[source]

Define a new tex macro with \newcommand. This will result in:

\newcommand{%(macro)s} { %(value)s}
newline()[source]

Append an newline to the texfile

class versuchung.tex.PgfKeyDict(filename='data.tex', pgfkey='/versuchung', setmacro='pgfkeyssetvalue')[source]

Can be used as: input parameter and output parameter

PgfKeyDict is very similar to Macros, but instead of \newcommand directives it uses pgfkeys, can be used as a dict and it is possible to read it in again to produce the (almost) same dict again.

>>> from versuchung.tex import PgfKeyDict
>>> pgf = PgfKeyDict("/tmp/test.tex")
>>> pgf["abcd"] = 23
>>> pgf.flush()  # flush method of File
>>> print(open("/tmp/test.tex").read())
\pgfkeyssetvalue{/versuchung/abcd}{23}

In the TeX source you can do something like:

\usepackage{tikz}
\pgfkeys{/pgf/number format/.cd,fixed,precision=1}
[...]
\newcommand{\versuchung}[1]{\pgfkeysvalueof{/versuchung/#1}}
\versuchung{abcd}

Note

It is better to use PgfKeyDict instead of Macros, because you can also use spaces and other weird characters in pgfkeys, which cannot be used in TeX macro names.

after_read(value)[source]

To provide filtering of file contents in subclasses, overrwrite this method. It is gets the file content as a string and returns the value()

before_write(value)[source]

To provide filtering of file contents in subclasses, overrwrite this method. This method gets the value() and returns a string, when the file is written to disk

flush()[source]

Flush the cached content of the file to disk

pandas(df, prefix='', names=None, verbose=False)[source]

Import pandas.DataFrame or pandas.Series as keys

This functions imports all cells from a DataFrame or Series as individual keys. The key is generated from the index and the columns (lines first, columns second). MultiIndex is supported for both dimension. For example, for the following DataFrame

>>> import pandas as pd
>>> df = pd.DataFrame([[1,1,1], [4, 1.5, 4]], columns=['th', 'speedup', 'load'])
>>> df = df.set_index('th')
>>> df
    speedup  load
th
1       1.0     1
4       1.5     4
>>> from versuchung.tex import PgfKeyDict
>>> pgf = PgfKeyDict("/tmp/test.tex")
>>> pgf.pandas(df, names=['th'], verbose=True)
th=1/speedup => 1.0
th=1/load => 1.0
th=4/speedup => 1.5
th=4/load => 4.0

With the names parameter, you can control whether a level should be prefixed with the index name. In the above example, th= is the result of the names parameter. A useful pattern is to describe() a column:

>>> pgf.pandas(df.speedup.describe(), prefix="speedup", verbose=True)
speedup/count => 2.0
speedup/mean => 1.25
speedup/std => 0.3535533905932738
speedup/min => 1.0
speedup/25 percent => 1.125
speedup/50 percent => 1.25
speedup/75 percent => 1.375
speedup/max => 1.5
class versuchung.tex.DatarefDict(filename='data.tex', key='')[source]

Can be used as: input parameter and output parameter

DatarefDict is like PgfKeyDict, but generates keys for dataref <https://ctan.org/pkg/dataref>.

In LaTeX, you can reference dataref keys or calculate with them:

\dref{/base}
\drefrel[percent of=/base]{/derived}
class versuchung.tex.LuaTable(filename='data.lua', experiment_name='experiment')[source]

Can be used as: input parameter and output parameter

When using Lua(La)TeX or lmtx (from ConTeXt), TeX is able to interact with Lua. This class exports the data as a lua table, so it can be accessed from TeX via Lua.

>>> from versuchung.tex import LuaTable
>>> lua = LuaTable("/tmp/table.lua")
>>> lua["some"]["key"] = 5
>>> lua.flush()  # flush method of File
>>> print(open("/tmp/table.lua").read())
userdata = userdata or {}
userdata.experiment = {
  some = {
    key = 5
  }
}

In ConTeXt, you can access the table with something like:

\startluacode
require("table.lua")
\stopluacode

\starttext
\ctxlua{context(userdata.experiment.some.key)}
\stoptext

In LuaLaTeX source you can do something like:

\documentclass{article}

\usepackage{luacode}
\luadirect{require("table.lua")}

\begin{document}
    \luadirect{tex.print(userdata.experiment.some.key)}
\end{document}

Note

If you want to calculate with the data even more and want to do that in your TeX project, a Lua table might be the best choice since you can use everything from Lua to use the data.

after_read(value)[source]

To provide filtering of file contents in subclasses, overrwrite this method. It is gets the file content as a string and returns the value()

before_write(value)[source]

To provide filtering of file contents in subclasses, overrwrite this method. This method gets the value() and returns a string, when the file is written to disk

flush()[source]

Flush the cached content of the file to disk