Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

I want to show the text from a Python Interpreter in a Windows console. This should be plain text (like you see in a command line terminal). I also want to show Python code snippets with highlighting. To do this, I set my default listing style for coloring (\lstset) and then created a custom listing style for command line terminal output (\lstdefinestyle).

When I try to use a framed box to show the terminal output, I find that it tries to fully justify the lines and adds extra spaces between words. How do I globally set all framed boxes to be left justified so I don't have to justify each box? Here is what I did:

\documentclass[openright,12pt]{book}
%Include Packages
\usepackage{hyperref}
\usepackage{float}
\usepackage{longtable}
\usepackage{listings} %code snippets
\usepackage{color}
\usepackage{calc}
\usepackage{amsmath}
\usepackage{framed} %console output displays
\usepackage{graphicx}
\usepackage{tikz}
\pagestyle{headings}

%Syntax Coloring for code snippets
\lstset{
  belowcaptionskip=1\baselineskip,
  breaklines=true,
  frame=l,
  xleftmargin=\parindent,
  language=Python,
  showstringspaces=false,
  basicstyle=\footnotesize\ttfamily,
  keywordstyle=\bfseries\color{green!40!black},
  commentstyle=\itshape\color{mygray},
  identifierstyle=\color{blue},
  stringstyle=\color{orange},
  numbers=left,
  numbersep=15pt, 
  numberstyle=\tiny\color{gray},
}

%Syntax coloring for inline code at the console
\lstdefinestyle{consolePy}{
  breaklines=true,
  language=Python,
  showstringspaces=false,
  xleftmargin=0pt,
  frame=none,
  numbers=none,
  identifierstyle=\ttfamily\color{black},
  basicstyle=\footnotesize\color{black},  
  commentstyle=\ttfamily\color{black},
  stringstyle=\ttfamily\color{black},
  keywordstyle=\ttfamily\color{black}
  }

\begin{document}
\begin{framed}
\begin{lstlisting}[style=consolePy]
>>> MyName = “Jason”
>>> print MyName
Jason
>>> MyAge = 30
>>> print 'My name is', MyName, 'and I'm', MyAge, 'years old.'
My name is Jason and I’m 30 years old.
\end{lstlisting}
\end{framed}
\end{document}

I would show a screen capture of the result but I'm new so I can't post images. Just know that the result is my text showing large white spaces in the framed listing. Thank you for any guidance you can provide!

share|improve this question
    
Welcome to TeX.sx! Please add a minimal working example (MWE) that illustrates your problem. It will be much easier for us to reproduce your situation and find out what the issue is when we see compilable code, starting with \documentclass{...} and ending with \end{document}. –  Marco Daniel Mar 8 '13 at 16:52
    
I'm sorry about that. I just changed the OP to include a working example. Thanks for your help. –  Jason Lillywhite Mar 8 '13 at 20:22
    
I can't see any differences. The package framed doesn't influence the justification. –  Marco Daniel Mar 8 '13 at 20:25
    
The following expression is justified and thus has huge white space when I print to pdf: >>> print 'My name is', MyName, 'and I'm', MyAge, 'years old.' –  Jason Lillywhite Mar 8 '13 at 20:48
    
@JasonLillywhite You can include an image; just use the interface and remove the ! that's added in front of the link; a user with enough rep will add it back. Does adding columns=fullflexible to your \lstset help? –  egreg Mar 9 '13 at 11:12

1 Answer 1

You want to use the option

columns=fullflexible

so that letters are stuck together as in normal typewriter type.

Note also that you should use the dumb quotes " rather than , because listings doesn't really understand UTF-8.

\documentclass[openright,12pt]{book}
%Include Packages
\usepackage{hyperref}
\usepackage{float}
\usepackage{longtable}
\usepackage{listings} %code snippets
\usepackage{color}
\usepackage{calc}
\usepackage{amsmath}
\usepackage{framed} %console output displays
\usepackage{graphicx}
\usepackage{tikz}
\pagestyle{headings}

%Syntax Coloring for code snippets
\lstset{
  belowcaptionskip=1\baselineskip,
  breaklines=true,
  frame=l,
  xleftmargin=\parindent,
  language=Python,
  showstringspaces=false,
  basicstyle=\footnotesize\ttfamily,
  keywordstyle=\bfseries\color{green!40!black},
  commentstyle=\itshape\color{mygray},
  identifierstyle=\color{blue},
  stringstyle=\color{orange},
  numbers=left,
  numbersep=15pt, 
  numberstyle=\tiny\color{gray},
}

%Syntax coloring for inline code at the console
\lstdefinestyle{consolePy}{
  breaklines=true,
  language=Python,
  showstringspaces=false,
  xleftmargin=0pt,
  frame=none,
  numbers=none,
  identifierstyle=\ttfamily\color{black},
  basicstyle=\footnotesize\color{black},  
  commentstyle=\ttfamily\color{black},
  stringstyle=\ttfamily\color{black},
  keywordstyle=\ttfamily\color{black},
  columns=fullflexible, % <--- added!
  }

\begin{document}
\begin{framed}
\begin{lstlisting}[style=consolePy]
>>> MyName = "Jason"
>>> print MyName
Jason
>>> MyAge = 30
>>> print 'My name is', MyName, 'and I'm', MyAge, 'years old.'
My name is Jason and I’m 30 years old.
\end{lstlisting}
\end{framed}
\end{document}

enter image description here

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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