Super User is a question and answer site for computer enthusiasts and power users. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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!

share|improve this question
up vote 11 down vote accepted

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.

share|improve this answer

You'll want to use the verbatim environment:

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.

share|improve this answer

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.

share|improve this answer

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!")!

share|improve this answer

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

share|improve this answer

You must log in to answer this question.

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