20

TeX is frustrating me. It has only the basics - and even then, I have to search how to divide two numbers.

The question is this: can I write my macros in more convenient language like Ruby or Lisp?

UPD: I will explain my situation. I'm writing a rulebook for my RPG module, and I need to construct an equipment table. Problem is, my rules are not the simplest ones:

http://pastebin.com/Z4Q9TQa5

I want nice fonts, so I use XeLaTeX. I am Russian, so I need polyglossia or babel.

Please note that every suggestion is tested against XeLaTeX, not LaTeX.

4
  • 6
    Broad answer 'no', but look at LaTeX3's expl3 language as a richer set of tools built on top of TeX.
    – Joseph Wright
    Commented Oct 16, 2012 at 11:22
  • 11
    Or switch to LuaTeX and use lua. Commented Oct 16, 2012 at 11:25
  • 2
    May you show an example of a situation in which such programming features are needed?
    – egreg
    Commented Oct 16, 2012 at 11:43
  • See also: question about Python in particular, lt3luabridge (execute Lua code from PDFLaTeX), ctan topic ctan.org/topic/callback and ctan.org/topic/exec-foreign. There's also Rmarkdown/knitr (preprocess generate LaTeX code) although I don't use them myself
    – user202729
    Commented Aug 7, 2022 at 1:25

4 Answers 4

19

My PythonTeX package is another Python-based option. PythonTeX was primarily developed for mathematical and scientific applications, but it's also suitable for much general-purpose work. I've included a few basic examples of creating macros with PythonTeX below. You do have to be careful about catcodes if you are working with special characters.

\documentclass{article} 
\usepackage{pythontex}

\begin{document}

Basic math:  \py{5324/251}

Macros that more closely interact with TeX:

\newcommand{\reverse}[1]{\py{"#1"[::-1]}}

\reverse{A sentence!}

\newcommand{\listtotabular}[1]{
    \begin{tabular}{|c|c|c|c|}
    \hline
    \py{'&'.join("#1".split(','))} \\
    \hline
    \end{tabular}
}

\listtotabular{First, Second, Third, Fourth}

\end{document}

enter image description here

In addition to the examples above, and the examples included with the documentation, you might also look at the following examples.

3
  • It's very buggy. Did you know that 'newfloat' package is actially distributed as 'caption'? I couldn't run the example, some obscure pgf error (pgfopts is installed) about /PYTX. Commented Oct 17, 2012 at 2:35
  • I'd overlooked that you need XeLaTeX. The example above works with XeLaTeX (at least with Windows with a fresh TeX Live 2012 installation). But the example file from GitHub won't work, unless you remove the inputenc and fontenc packages, load fontspec and use \defaultfontfeatures{Ligatures=TeX}, and run xelatex with the -8bit option. The -8bit option is needed for handling tabs; see this.
    – G. Poore
    Commented Oct 17, 2012 at 5:06
  • Thanks; it gave me a mountain of errors, but worked. Seems like it needs strict TL2012. Commented Oct 17, 2012 at 7:45
11
2
  • PerlTeX is not working with XeTeX, and LuaLaTeX has known issues with non-Latin languages. I couldn't start python package, it gives a "can't find file `test.py.out'." error, but I'll keep digging that way. Commented Oct 17, 2012 at 2:09
  • Links are broken. Could you be kind to update?
    – Foad
    Commented May 5 at 5:35
7

While I haven't tried, you might be able to use bashful to interact with programs written in other languages via stdin and stdout.

I had no problems with a simple example as long as I compiled with -shell-escape:

\documentclass{article}
\usepackage{bashful}
\def\fortune{\splice{fortune}}
\begin{document}
\fortune

\fortune
\end{document}

I am not sure about the polyglossia issue that is mentioned in the documentation.

2
  • Seems like it hacks Polyglossia, causing it to use latin monospaced font. Also gives me an error because "File test.stdout was empty" Commented Oct 17, 2012 at 2:19
  • @AlexanderYakovlev I could get a simple example running no problem
    – StrongBad
    Commented Oct 17, 2012 at 13:40
5

Now you can try functional package. It provides a LaTeX2 interface for expl3 and you can do functional programming similar to other modern programming languages such as Lua. The evaluation of composite functions is from inside to outside.

Here is a simple example showing the similarity between functional code and lua code:

\documentclass{article}
\usepackage{functional}
\begin{document}

%% Functional code for comparison

\IgnoreSpacesOn
\PrgNewFunction \MathSquare { m } {
  \IntSet \lTmpaInt { \IntEval {#1 * #1} }
  \Result { \Value \lTmpaInt }
}
\IgnoreSpacesOff
\MathSquare{5}
\MathSquare{\MathSquare{5}}

\end{document}
-- Lua code for comparison --

-- define a function --
function MathSquare (arg)
  local lTmpaInt = arg * arg
  return lTmpaInt
end
-- use the function --
print(MathSquare(5))
print(MathSquare(MathSquare(5)))

Note that with this package, you need to pass return values of functions with \Result command.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .