GeneralHow to fit long code?

LaTeX specific issues not fitting into one of the other forums of this category.
Post Reply
carol
Posts: 105
Joined: Wed Dec 24, 2008 7:25 pm

How to fit long code?

Post by carol »

Hi,
Using listing or verbatim package, the long codes would go into page margin. How is it possible to format long codes?

Look forward to your reply,

Recommended reading 2024:

LaTeXguide.org • LaTeX-Cookbook.net • TikZ.org

NEW: TikZ book now 40% off at Amazon.com for a short time.

localghost
Site Moderator
Posts: 9202
Joined: Fri Feb 02, 2007 12:06 pm

How to fit long code?

Post by localghost »

I see no problem with listings.

Code: Select all

\documentclass[11pt,a4paper,english]{article}
\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}
\usepackage{babel}
\usepackage[svgnames]{xcolor}
\usepackage{listings}
\usepackage{lmodern}

\lstset{
  aboveskip=1ex,
  backgroundcolor=\color{gray!25},
  basicstyle=\small\ttfamily,
  belowskip=1ex,
  breaklines=true,
  columns=fullflexible,
  framerule=0pt,
  framexrightmargin=0em,
  framexleftmargin=0em,
  numbers=left,
  numberstyle=\footnotesize\sffamily,
  tabsize=2
}

\begin{document}
  \lstinputlisting{minimal.cls}
\end{document}
The used files.

Code: Select all

 *File List*
 article.cls    2007/10/19 v1.4h Standard LaTeX document class
  size11.clo    2007/10/19 v1.4h Standard LaTeX file (size option)
 fontenc.sty
   t1enc.def    2005/09/27 v1.99g Standard LaTeX file
inputenc.sty    2008/03/30 v1.1d Input encoding file
  latin1.def    2008/03/30 v1.1d Input encoding file
   babel.sty    2008/07/06 v3.8l The Babel package
 bblopts.cfg    2006/07/31 v1.0 MiKTeX 'babel' configuration
 english.ldf    2005/03/30 v3.3o English support from the babel system
  xcolor.sty    2007/01/21 v2.11 LaTeX color extensions (UK)
   color.cfg    2007/01/18 v1.5 color configuration of teTeX/TeXLive
  pdftex.def    2009/08/25 v0.04m Graphics/color for pdfTeX
  svgnam.def    2007/01/21 v2.11 Predefined colors according to SVG 1.1 (UK)
listings.sty    2007/02/22 1.4 (Carsten Heinz)
  keyval.sty    1999/03/16 v1.13 key=value parser (DPC)
 lstmisc.sty    2007/02/22 1.4 (Carsten Heinz)
listings.cfg    2007/02/22 1.4 listings configuration
 lmodern.sty    2009/10/30 v1.6 Latin Modern Fonts
   t1lmr.fd    2009/10/30 v1.6 Font defs for Latin Modern
supp-pdf.tex
  t1lmtt.fd    2009/10/30 v1.6 Font defs for Latin Modern
 minimal.cls
  t1lmss.fd    2009/10/30 v1.6 Font defs for Latin Modern
 ***********

Best regards
Thorsten
carol
Posts: 105
Joined: Wed Dec 24, 2008 7:25 pm

Re: How to fit long code?

Post by carol »

Thanks for your quick reply.

How about if one or few lines code is used that doesn't worth to be put in a separate file? If we use long code in \begin{lstlisting} ...\end{lstlisting}, it won't be formatted
User avatar
localghost
Site Moderator
Posts: 9202
Joined: Fri Feb 02, 2007 12:06 pm

How to fit long code?

Post by localghost »

I still see no problem.

Code: Select all

\listfiles
\documentclass[11pt,a4paper,english]{article}
\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}
\usepackage{babel}
\usepackage[svgnames]{xcolor}
\usepackage{listings}
\usepackage{lmodern}

\lstset{
  aboveskip=1ex,
  backgroundcolor=\color{gray!25},
  basicstyle=\small\ttfamily,
  belowskip=1ex,
  breaklines=true,
  columns=fullflexible,
  framerule=0pt,
  framexrightmargin=0em,
  framexleftmargin=0em,
  language=[LaTeX]TeX,
  numbers=left,
  numberstyle=\footnotesize\sffamily,
  tabsize=2
}

\begin{document}
  \begin{lstlisting}
\documentclass[11pt,a4paper,english]{article}
\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}
\usepackage{babel}
\usepackage[svgnames]{xcolor}
\usepackage{listings}
\usepackage{blindtext}
\usepackage{lmodern}

\lstset{
  aboveskip=1ex,
  backgroundcolor=\color{gray!25},
  basicstyle=\small\ttfamily,
  belowskip=1ex,
  breaklines=true,
  columns=fullflexible,
  framerule=0pt,
  framexrightmargin=0em,
  framexleftmargin=0em,
  numbers=left,
  numberstyle=\footnotesize\sffamily,
  tabsize=2
}

\begin{document}
  \blindtext   % A long comment line just to show that line breaks are possible
\end{document}
  \end{lstlisting}
\end{document}
carol
Posts: 105
Joined: Wed Dec 24, 2008 7:25 pm

How to fit long code?

Post by carol »

How about if the code is simply used in \begin{lstlisting} ... \end{lstlisting} is simply used? How to format in this case?

Cheers,

Code: Select all

\documentclass[12pt,a4paper]{report}
\usepackage{listings}
\begin{document}
 \begin{lstlisting}
 this is a very long long long code that can't be formatted by lstlisting 
\end{lstlisting}
\end{document}
User avatar
localghost
Site Moderator
Posts: 9202
Joined: Fri Feb 02, 2007 12:06 pm

How to fit long code?

Post by localghost »

Do you actually read what people are writing to you? Or package manuals? The issue has been solved long since my first reply and at least since my second one. Did you test the sample code? It's just about one single setting.

Code: Select all

\documentclass[12pt,a4paper]{article}
\usepackage{listings}

\lstset{%
  breaklines=true
}

\begin{document}
\begin{lstlisting}
this is a very long long long code that can't be formatted by lstlisting
\end{lstlisting}
\end{document}
Just learn to read package manuals. They explain all the features of a package.
Post Reply