General ⇒ How to fit long code?
How to fit long code?
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,
Learn LaTeX easily with newest books:
The LaTeX Beginner's Guide: 2nd edition and perfect for students writing a thesis
The LaTeX Cookbook: 2nd edition full of practical examples for mathematics, physics, chemistry, and more
LaTeX Graphics with TikZ: the first book about TikZ for perfect drawings in your LaTeX thesis
- localghost
- Site Moderator
- Posts: 9202
- Joined: Fri Feb 02, 2007 12:06 pm
How to fit long code?
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}
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
Board Rules
Avoidable Mistakes
¹ System: TeX Live 2025 (vanilla), TeXworks 0.6.10
Re: How to fit long code?
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
- localghost
- Site Moderator
- Posts: 9202
- Joined: Fri Feb 02, 2007 12:06 pm
How to fit long code?
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}
Board Rules
Avoidable Mistakes
¹ System: TeX Live 2025 (vanilla), TeXworks 0.6.10
How to fit long code?
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}
- localghost
- Site Moderator
- Posts: 9202
- Joined: Fri Feb 02, 2007 12:06 pm
How to fit long code?
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}
Board Rules
Avoidable Mistakes
¹ System: TeX Live 2025 (vanilla), TeXworks 0.6.10