# Python code in LaTeX not running

## The Latex/Python Project:

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

## Problem:

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
\documentclass{article}
\usepackage{hyperref}
\usepackage{python}
\usepackage[final]{pdfpages}
\usepackage[ngerman]{babel}

\begin{document}
\begin{python}
import os
from Tkinter import Tk

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

# get filenames from path

for name in v5:

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

\tableofcontents
\end{document}

-
Welcome to TeX.SX! Usually, we don't put a greeting or a “thank you” in our posts. While this might seem strange at first, it is not a sign of lack of politeness, but rather part of our trying to keep everything very concise. Accepting and upvoting answers is the preferred way here to say “thank you” to users who helped you. –  Martin Schröder Oct 6 '13 at 19:58

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)

-
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 Oct 6 '13 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 Oct 7 '13 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 Oct 7 '13 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 Oct 10 '13 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 Oct 10 '13 at 21:40