3

I'm using listings for a document which contains code examples within the TeX file and also inputed from external files. I'd like horizontal lines above and below the latter but not the former, but with my current set-up I get them for both. My preamble has

 \lstset{
         basicstyle=\footnotesize\ttfamily,
         escapechar=¢,
         language=python,
         frame=lines
 }

I know I can over-ride the frame for lstlisting with \begin{lstlisting}[frame=none] but is there a way to do it automatically without having to do this?

I've got lots and lots of listings, so I'd prefer change the preamble but not to change the main document text if possible.

Here's an example of how it works at the moment:

\documentclass{article}

\usepackage{filecontents}
\begin{filecontents*}{code.py}
for j in range(100):
    print(j)
    print(j**3)
\end{filecontents*}

\usepackage{listings}
\lstset{
         basicstyle=\footnotesize\ttfamily,
         escapechar=¢,
         language=python,
         frame=lines
 }

\begin{document}
An inline code example:
\begin{lstlisting}
for i in range(10):
    print(i**2)
\end{lstlisting}

Some code inputted from elsewhere:
\lstinputlisting[caption=code example]{code.py}

\end{document}

As you can see, I get lines around both code listings, but I don't want them around the first one.

  • Welcome to TeX.SX! Please help us to help you and 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}. – user31729 Jan 2 '15 at 2:27
  • OK! I've edited my question with an example. – xnx Jan 2 '15 at 2:34
  • @xnx Mike's approach is sound. You should only leave options that are common to all kinds of listings in \lstset{...}, and use specific options in a custom listing command and a custom listing environment that you define. That makes sense from a semantic point of view and is maintainable. – jub0bs Jan 2 '15 at 11:05
4

Redefine \lstinputlisting to always use frame=lines:

enter image description here

\documentclass{article}

\usepackage{filecontents}
\begin{filecontents*}{code.py}
for j in range(100):
    print(j)
    print(j**3)
\end{filecontents*}

\usepackage{listings}
\lstset{
   basicstyle=\footnotesize\ttfamily,
   escapechar=¢,
   language=python
}
\let\oldlstinputlisting\lstinputlisting
% \lstinputlisting always have frame=lines
\renewcommand{\lstinputlisting}[2][]{\oldlstinputlisting[frame=lines,#1]{#2}}

\begin{document}
An inline code example:
\begin{lstlisting}
for i in range(10):
    print(i**2)
\end{lstlisting}

Some code inputted from elsewhere:
\lstinputlisting[caption=code example]{code.py}

\end{document}
  • Why not define a custom \lstinputlisting instead of overwriting the original definition? – jub0bs Jan 2 '15 at 11:07
  • I could, but then I'd have to change it in lots of source tex files; I could script this, but I'm afraid of messing something up. – xnx Jan 2 '15 at 20:06
2

The \lstnewenvironment can be used to define alternate options, but you'll need to use the new environment instead of the lstlisting one.

enter image description here

\documentclass{article}
\begin{filecontents*}{code.py}
for i in range(10):
    print(i**2)
\end{filecontents*}
 \usepackage{listings}
\lstset{
         basicstyle=\footnotesize\ttfamily,
         escapechar=¢,
         language=python,
         frame=lines
 }
\lstnewenvironment{unlined}
{\lstset{frame=none}}
{} % http://mirrors.ctan.org/macros/latex/contrib/listings/listings.pdf , section 4.16

\begin{document}
An inline code example:
\begin{unlined}
for i in range(10):
    print(i**2)
\end{unlined}

Some code inputted from elsewhere:
\lstinputlisting[caption=code example]{code.py}

\end{document}

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.