The Latex/Python Project:

Create in Latex a songbook from pdf files in a folder and make a toc.


The code below is not running in Latex. Thanks to G.Poore I added the \verb command to overcome problems with underscores etc. Still, it doesnt work. I think the problems are:

  1. Latex is somehow removing the indentation
  2. There might be still some other coding mistakes which I cant test because of 1. (sorry...)

usepackage python vs pythontex:

I am using python because

  1. Its easier just to run one file than latex/python/latex
  2. for some reasons pythontex is not running on my mac, but no problem, see 1.

Can anybody give me some hints? Thanks!

  % !TEX program = XeLaTeX+shell-escape

import os
from Tkinter import Tk
from tkFileDialog import askdirectory

Tk().withdraw() # we don't want a full GUI, so keep the root window from appearing

# show an "Open" dialog box and return the path to the selected file
Liederpfad = askdirectory(title="Choose folder:") 

# get filenames from path
v5 = os.listdir(Liederpfad)

# remove "symlink" if there
for name in v5:
    if name.endswith("symlink"):
        os.rename(Liederpfad + "/" + name, Liederpfad + "/" + name[:-8])

# write latex code
for x in v5:
    if not x.startswith("."): #versteckte Systemdateien ausblenden
        print ('\\phantomsection')
        if x.endswith("symlink"):
            print ('\\addcontentsline{toc}{section}{\\verb|' + x[:-12] + '|}\n')
            print ('\\addcontentsline{toc}{section}{\\verb|' + x[:-4] + '|}\n')
        print ('\\includepdf[fitpaper=true,pages=-,pagecommand={\\thispagestyle{fancy}}]{\\verb|' + Liederpfad + '/' + x + '|}\n)
        print ('')


1 Answer 1


In your short example, you are trying to print file names. But in general, file names will not be valid LaTeX, since they may contain underscores, etc. If you do something like

print('\\verb|' + name + '|\n')

then the file names will be wrapped in a \verb command and thus special characters won't be an issue.

Also, you don't need the % after the python environment.

Depending on what you ultimately want to do, you may wish to consider the pythontex package. It provides a lot of additional features, but also involves a three-step compile process (LaTeX, pythontex.py, and then LaTeX again)

  • 1
    Pythontex is definitely the best way to integrate python and TeX. Can one in principle with LuaTeX avoid the three-step process by calling python from Lua's os module?
    – Alexander
    Commented Oct 6, 2013 at 22:09
  • @Alexander The 3-step process allows code from multiple environments/commands to be collected and then executed in a single session. In principle, I believe it would be possible to do a single-step process using LuaTeX. Each bit of Python code would be checked to see if it was modified. If not, a cached version of output would be loaded; otherwise, all the preceding code in that session plus the current code would have to be executed, and the session would have to be available for later code (some sort of server?). It should be possible, but it would be complex.
    – G. Poore
    Commented Oct 7, 2013 at 0:21
  • @Alexander On further thought, a LuaTeX version would actually be slower under some circumstances. In the current approach, all modified Python code is executed at once (step 2 of the process), and sessions run in parallel. A LuaTeX version would have to run all Python code in serial as the document was processed. If only a single session is modified, a LuaTeX version would save the time of one TeX compile. If multiple sessions must be executed, the current approach would be significantly faster in some cases.
    – G. Poore
    Commented Oct 7, 2013 at 0:31
  • @G.Poore Thanks a lot! When I tried the pythontex package with a simple example it only produced "??" in the output. What could be the problem? Thanks! \documentclass[12pt]{scrartcl} \usepackage[T1]{fontenc} \usepackage[utf8]{inputenc} \usepackage{pythontex} \begin{document} \py{2+2} \end{document}
    – Andreas
    Commented Oct 10, 2013 at 19:15
  • @Andreas The ?? is a placeholder until code is actually executed. Unlike the python package, pythontex doesn't use \write18 to run code during document compilation. Compiling a document with pythontex is a three-step process: run LaTeX, then run pythontex.py <file>.tex, then run LaTeX again.
    – G. Poore
    Commented Oct 10, 2013 at 21:40

You must log in to answer this question.

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