Maybe I'm just being thick, but I can't find any sensible way to simply paste in my (python) code into latex without losing all the indent information (kinda important for python).

Anyone got any bright ideas? I'm not worried about syntax highlighting; all I want is my tab key back!


My favorite environment for this minted. I use it to input entire source files such as:


You can also use it without inputing a file, and more importantly, define how much indent it gobbles once pasted:

    print('I am a Python script')

That way your LaTeX is still nicely indented, but your verbatim code is not. This also provides syntax-highlighting, which I know you mentioned you weren't interested in. Just don't define the language.


You'll want to use the verbatim environment: http://web.mit.edu/vogt/www/latex/ltx-79.html

If the problem is immediately when you paste it in... are you using an IDE to make the document? Try just opening the file in a plain text editor like gedit or notepad.


I'd go with minted, as fideli suggested, but it's good to be aware of the listings package, which is a pure Latex solution ot the problem.

minted is derived from Pygments, a source highlighter written in Python. minted coimes with a Latex bridge, based on \write18, and the texments package provides one for Pygments. There's no downside to minted compared to Pygments that I know of: maybe the two will be merged at some point.


To use the listings package, you'll have to include the package and load necessary languages in the preamble:


Set some options inside the document:


... and then you can either give snippets inline:

  print ("Hello, World!")

... or read them from a file:


... or write the code inline: \lstinline!print ("Hello, World!")!


For LyX, see this (imports a child document using the "listing" format).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.