1

I write a thesis and want to have my code/textfile in the appendix. I dont want to copy the code, I only want to write the path of my file into tex.

I already tried \verbatiminput{main.py} and \lstinputlisting[language=Python]{main.py} but I need to have the line numbered.

Needed features

  • numbering lines
  • load from textfile (not copying)

Nice to have features

These features would be nice, but I can manage to work around.

  • use tab as indent
  • break line, if line is to long

Not necessary at all, but very nice to have

Is there a simple way to highlight python syntax?


Edit

As MadyYuvi sugest, I tried \lstinputlisting. In one file I got an umalut (ä), so it failed to compile.
! Package inputenc Error: Invalid UTF-8 byte "A4.
In a german forum I found an suggestion to use \usepackage{listingsutf8} but it does not help.

Edit

minimal working example (MWE)

document.tex
\documentclass{article}
\usepackage{listingsutf8}

\begin{document}

\lstinputlisting[numbers=left,numberstyle=\tiny,language=Python,breaklines=true,breakatwhitespace=true,breakindent=12pt,]{main.py}

\end{document}
main.py
#!/usr/bin/env python3
print('I have got umlauts like ä,ö,ü in my code.')
1
  • try with minted.sty
    – MadyYuvi
    Jul 13, 2020 at 9:20

1 Answer 1

1

May be this helps..,

\documentclass{book}
\usepackage{listings}

\begin{document}

\lstinputlisting[numbers=left,numberstyle=\tiny,language=Python,breaklines=true,breakatwhitespace=true,breakindent=12pt,]{test.py}

\end{document}

Updation

If you have any special characters in programming code, then try with the below MWE:

\documentclass{article}
\usepackage[T2A,T1]{fontenc}
\usepackage{listingsutf8}
\begin{document}

\lstinputlisting[numbers=left,numberstyle=\tiny,language=Python,breaklines=true,breakatwhitespace=true,breakindent=12pt]{test.py}

\end{document}

Output

enter image description here

Compile this file "XeLaTeX", this is because of font issues...

Also, refer this, Ulrike and egreg gave their wonderful suggestion, Characters are not displayed correctly by using FiraMono font (dvips -> ps2pdf)

6
  • That works for some files without umlauts in it. Do you know how to change it for umlauts?
    – Boba Fit
    Jul 13, 2020 at 10:08
  • Where does the umlauts come? Inside program code??
    – MadyYuvi
    Jul 13, 2020 at 12:52
  • Yes I got some in my programcode.
    – Boba Fit
    Jul 13, 2020 at 22:43
  • Can you post a MWE on your code for better understanding ?....
    – MadyYuvi
    Jul 14, 2020 at 4:38
  • 1
    I edited my question with the MWE based on your answer and a python file. Does this help you to understand my problem?
    – Boba Fit
    Jul 14, 2020 at 8:03

You must log in to answer this question.

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