29

In tex environment, when I try to use the verbatim function using verbatim packages, sometimes the longer text in the line tend to be cut off. How can I resize the text to fit into the line or automatically extend the text to the next line so as to retain all the text. For example I have the following text:

    \begin{verbatim}
        C **  RAINCVT   =CONVERTS RAIN TO UNITS OF M/SEC , inch/day=0.0254m/86400s=2.94E-7m/s, inch/h=7.05556E-6m/s
C **  EVAPCVT   =CONVERTS EVAP TO UNITS OF M/SEC, IF EVAPCVT<0 EVAP IS INTERNALLY COMPUTED
C **  SOLRCVT   =CONVERTS SOLAR SW RADIATION TO JOULES/S/SQ METER (Watts/m^2)
    \end{verbatim}

In my output the first line doesn't show all the text. I would also appreciate if someone could tell how to limit the floating verbatim ? In tables using the label [h] at the end would fix the position of the table which doesn't seem to work on verbatim.

Jdbaba
  • 2,643

1 Answers1

27

The standard verbatim environment is very basic. To have automatic line breaking, use the listings package and its breaklines=true option:

\documentclass[11pt]{article}
\usepackage{listings}
\lstset{
basicstyle=\small\ttfamily,
columns=flexible,
breaklines=true
}

\begin{document}

With the standard \texttt{verbatim} environment:
\begin{verbatim}
        C **  RAINCVT   =CONVERTS RAIN TO UNITS OF M/SEC , inch/day=0.0254m/86400s=2.94E-7m/s, inch/h=7.05556E-6m/s
C **  EVAPCVT   =CONVERTS EVAP TO UNITS OF M/SEC, IF EVAPCVT<0 EVAP IS INTERNALLY COMPUTED
C **  SOLRCVT   =CONVERTS SOLAR SW RADIATION TO JOULES/S/SQ METER (Watts/m^2)
    \end{verbatim}

With the \texttt{lstlisting} environment and \texttt{breaklines=true}:
\begin{lstlisting}
        C **  RAINCVT   =CONVERTS RAIN TO UNITS OF M/SEC , inch/day=0.0254m/86400s=2.94E-7m/s, inch/h=7.05556E-6m/s
C **  EVAPCVT   =CONVERTS EVAP TO UNITS OF M/SEC, IF EVAPCVT<0 EVAP IS INTERNALLY COMPUTED
C **  SOLRCVT   =CONVERTS SOLAR SW RADIATION TO JOULES/S/SQ METER (Watts/m^2)
    \end{lstlisting}

\end{document}

enter image description here

Gonzalo Medina
  • 505,128
  • Small note, in Texmaker4.4 with MIktex 2.9, unlike the \begin{verbatim}sometext\end{verbatim}, the content between the \begin{lstlisting} start on a new line \end{lstlisting} should start on a new line. It will throw errors if the begin and end listing are in the same line with some text in between. Also, that setting added line numbers to the verbatim text when using lstlisting.This could be ommitted with: \begin{lstlisting}[numbers=none]. – a.t. Aug 22 '20 at 13:26
  • This is a great solution...but lstlisting as a name? How can one remember this? Any recommendations on how to best alias this or redefine verbatim to be this? – ashman Dec 14 '20 at 05:59