formatting - How to fix code examples in Python documentation?

07
2014-07
  • Cybis

    I'm having a strange problem when trying to view the Python documentation. Basically, all code examples cut off the last line of text. The screenshot below illustrates what I mean. The text box isn't large enough to show the definition for "type". There is no way to scroll that text box down or to expand it, so its impossible for me to read that line.

    How do I fix this? Is there any way I can read that last line? It happens on all of the multiline code examples in the docs and is very annoying. I'm on Windows XP, using the Python 2.7 docs.

    Thank you for any help you can provide.

    EDIT:

    Thanks to martineau's answer below, I managed to figure out exactly what's happening. The horizontal scroll bar is obscuring the last line of text. By decreasing the font size (via both the 'Font' and 'Accessibility' settings martineau mentioned), the lines can be made to fit completely within the text box, thereby preventing the horizontal scroll bar from appearing.

    Whose bright idea was it to make the scroll bar obscure the last line in a textbox?

    Screenshot from Python documentation

  • Answers
  • martineau

    I, too, use Windows XP and encountered the same problem, which was very irritating since I tend to use Python's Help file a lot. After a lot of fiddling around and experimenting I was able to fix it up and it now looks like this:

    screenshot of Python Help window display

    This was accomplished by adjusting two of the settings under the Options / Internet Options... menu item. The first was to change the "Plain Text font" to something I like better (and use in my editor for writing code) by clicking on the Fonts button to bring up the Fonts dialog, as illustrated below:

    screenshot of fonts dialog

    To address the size issue, the second thing I did was to enable "Ignore font sizes specified on webpages" option in the Accessibility dialog which appears if when you click on the Accessibility button:

    screenshot of accessibility dialog

    Making these two changes also affects how things look in Internet Explorer, although not too badly, and besides I don't really care that much because I hardly ever use it -- your mileage may differ.

    Last but not least, you have to make sure you have the window's width set wide enough to prevent scroll-bars appearing next to the the blocks of code

    Hope this helps.

  • Gord Thompson

    FWIW, it's not just you (on XP). I get the same thing under Vista.

    Try this for a workaround: Click at the beginning of the code sample and drag down until you've selected all of the code and the first letter of the sentence following the code box (see screenshot below). Then, keeping the mouse button down, slide left until that final letter is no longer selected. You now have the entire code sample selected. Hit [Ctrl+C] to copy and then paste into Notepad (or similar).

    enter image description here


  • Related Question

    python - Adding programming code into LaTex / LyX
  • Andrew Bolster

    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!


  • Related Answers
  • fideli

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

    \inputminted[linenos,fontsize=\scriptsize]{python}{script.py}
    

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

    \newminted{python}{gobble=4,linenos,fontsize=\scriptsize}
    \begin{pythoncode}
        print('I am a Python script')
    \end{pythoncode}
    

    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.

  • oadams

    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.

  • Charles Stewart

    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.

  • RolKau

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

    \usepackage{listings}
    \lstloadlanguages{Python}
    

    Set some options inside the document:

    \lstset{language=Python,tabsize=2}
    

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

    \begin{lstlisting}
      print ("Hello, World!")
    \end{lstlisting}
    

    ... or read them from a file:

    \lstinputlisting[firstline=10,lastline=20]{Hello.py}
    

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