Text Formattingautomatically arrange code input

Information and discussion about LaTeX's general text formatting features (e.g. bold, italic, enumerations, ...)
Post Reply
SkyHiRider
Posts: 11
Joined: Mon Oct 18, 2010 2:14 pm

automatically arrange code input

Post by SkyHiRider »

I'm writing a work with lots of Linux terminal input and output commands. I'm using

Code: Select all

\begin{verbatim}

ip xfrm state add src 192.168.77.23 \
dst 192.168.77.24 proto esp spi 0x53fa0fdd \
mode transport reqid 16386 replay-window 32 \
auth "hmac(sha1)" \
0x55f01ac07e15e437115dde0aedd18a822ba9f81e \
enc "cbc(aes)" \
0x6aed4975adf006d65c76f63923a6265b \
sel src 192.168.77.23 dst 192.168.77.24; ip xfrm \
state add src 192.168.77.24 dst 192.168.77.23 \
proto esp spi 0x53fa0fdd mode transport reqid \
16386 replay-window 32 auth "hmac(sha1)" \
0x55f01ac07e15e437115dde0aedd18a822ba9f81e \
enc "cbc(aes)" \
0x6aed4975adf006d65c76f63923a6265b sel src \
192.168.77.24 dst 192.168.77.23; ip xfrm \
policy add dir out src 192.168.77.24 dst \
192.168.77.23 ptype main action allow priority \
2080 tmpl src 192.168.77.24 dst 192.168.77.23 \
proto esp reqid 16386 mode transport; ip xfrm \
policy add dir in src 192.168.77.23 dst \
192.168.77.24 ptype main action allow priority \
2080 tmpl src 192.168.77.23 dst 192.168.77.24 \
proto esp reqid 16386 mode transport

\end{verbatim}
to input the code but I have to manually add \ as terminal line breaks, as a single line is so long that it goes beyond the border of the document and is thus unreadable. When using \ the text is terrible to read and reduces the overall quality of the work.

Tried using \listings package but that can only highlight code, I haven't found a way yet to automatically make it fit the page.

Recommended reading 2024:

LaTeXguide.org • LaTeX-Cookbook.net • TikZ.org
LaTeX Beginner's Guide LaTeX Cookbook LaTeX TikZ graphics TikZによるLaTeXグラフィックス
SkyHiRider
Posts: 11
Joined: Mon Oct 18, 2010 2:14 pm

automatically arrange code input

Post by SkyHiRider »

I found this:

Code: Select all

\usepackage{listings}
\lstset{ %
language=C++,                % choose the language of the code
basicstyle=\footnotesize,       % the size of the fonts that are used for the code
numbers=left,                   % where to put the line-numbers
numberstyle=\footnotesize,      % the size of the fonts that are used for the line-numbers
stepnumber=1,                   % the step between two line-numbers. If it is 1 each line will be numbered
numbersep=5pt,                  % how far the line-numbers are from the code
backgroundcolor=\color{white},  % choose the background color. You must add \usepackage{color}
showspaces=false,               % show spaces adding particular underscores
showstringspaces=false,         % underline spaces within strings
showtabs=false,                 % show tabs within strings adding particular underscores
frame=single,                   % adds a frame around the code
tabsize=2,              % sets default tabsize to 2 spaces
captionpos=b,                   % sets the caption-position to bottom
breaklines=true,        % sets automatic line breaking
breakatwhitespace=false,    % sets if automatic breaks should only happen at whitespace
escapeinside={\%}{)}          % if you want to add a comment within your code
}
This works fine, but when I copy/paste the text it breaks and cannot be used in the terminal.

I need a way for listings to automatically add the \ character at the end of each line so that the code can be executed after it's pasted.
Post Reply