2

I have limited horizontal space to list some code:

\documentclass[]{article}
\usepackage{xcolor}
\usepackage{listings}
\definecolor{backcolour}{rgb}{0.95,0.95,0.92}
\lstdefinestyle{mystyle}{
    backgroundcolor=\color{backcolour},
    language=bash,
    basicstyle=\footnotesize\ttfamily,
    keepspaces=true,
}
\lstset{style=mystyle}
\begin{document}
\begin{lstlisting}
C_10 := elementwise.range_check(C_4,S_5,S_6)
\end{lstlisting}
\end{document}

and I want to squeeze it horizontally a bit. (Yes, I know in this example it extends to the end of the line; but maybe I have it in a minipage or in a multi-column document etc.)

Now, I don't want to go \tiny, just a bit of horizontal squeeze. If I weren't in an lstlistings environment, I might use something like a scalebox, or perhaps textls, but I don't really have that flexibility (I think) within an lstlistings environment. I'm assuming there must be something I need to put into the basicstyle package parameter in \lstset; but - what?

12
  • Have you checked the columns option for flexible vs. fixed width?
    – TeXnician
    Jul 7, 2017 at 11:14
  • @TeXnician: Is that relevant to fixed-width (\ttfamily) code?
    – einpoklum
    Jul 7, 2017 at 11:15
  • can't you just use \small ? or choose a different, less wide, monospace font. Squashing it horizontally only sounds like vandalism:-) Jul 7, 2017 at 11:18
  • Concerning the column options there's one page in the manual (section 2.10) you should read. But in most cases @DavidCarlisle is right that you might be better off choosing a different font.
    – TeXnician
    Jul 7, 2017 at 11:28
  • @DavidCarlisle: See edit.
    – einpoklum
    Jul 7, 2017 at 11:34

2 Answers 2

1

latin modern for example has light tt that's a bit thinner and condensed tt that's a lot thinner

enter image description here

\documentclass[]{article}
\usepackage{xcolor}
\usepackage{listings}
\makeatletter
\def\lmtt@use@light@as@normal{}
\definecolor{backcolour}{rgb}{0.95,0.95,0.92}
\lstdefinestyle{mystyle3}{
    backgroundcolor=\color{backcolour},
    language=bash,
    basicstyle=\fontencoding{T1}\footnotesize\fontfamily{lmtt}\fontseries{c}\selectfont,
    keepspaces=true,
}
\lstdefinestyle{mystyle2}{
    backgroundcolor=\color{backcolour},
    language=bash,
    basicstyle=\fontencoding{T1}\footnotesize\fontfamily{lmtt}\fontseries{m}\selectfont,
    keepspaces=true,
}
\lstdefinestyle{mystyle}{
    backgroundcolor=\color{backcolour},
    language=bash,
    basicstyle=\footnotesize,
    keepspaces=true,
}
\lstset{style=mystyle}
\begin{document}
\begin{lstlisting}
C_10 := elementwise.range_check(C_4,S_5,S_6)
\end{lstlisting}

\lstset{style=mystyle2}
\begin{lstlisting}
C_10 := elementwise.range_check(C_4,S_5,S_6)
\end{lstlisting}

\lstset{style=mystyle3}
\begin{lstlisting}
C_10 := elementwise.range_check(C_4,S_5,S_6)
\end{lstlisting}

\end{document}
1
  • you could perhaps use the condensed but increase the font size up from footnotesize.... Jul 7, 2017 at 12:30
1

Here is a way, with a narrower font, namely URW Letter Gothic (available on CTAN:

\documentclass[]{article}
    \usepackage[utf8]{inputenc}
    \usepackage[T1]{fontenc}
    \usepackage{xcolor}
    \usepackage{listings}
    \definecolor{backcolour}{rgb}{0.95,0.95,0.92}
    \lstdefinestyle{mystyle}{
        backgroundcolor=\color{backcolour},
        language=bash,
        basicstyle=\footnotesize\ttfamily,
        keepspaces=true,
    }
    \lstset{style=mystyle}
    \usepackage{etoolbox}
    \AtBeginEnvironment{lstlisting}{\renewcommand\ttfamily{\fontfamily{ulg}\selectfont}}

    \begin{document}

    \noindent\verb|C_10 := elementwise.range_check(C_4,S_5,S_6)|
    \begin{lstlisting}
    C_10 := elementwise.range_check(C_4,S_5,S_6)
    \end{lstlisting}

    \begin{lstlisting}[basicstyle=\scriptsize\ttfamily]
    C_10 := elementwise.range_check(C_4,S_5,S_6)
    \end{lstlisting}

    \end{document} 

enter image description here

You must log in to answer this question.

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