I want to generate a PDF of user-submitted text using LaTeX. To handle crazy user input, I first thought about using the verbatim package, but of course it doesn't break up too long lines. Is there some package that works similar to verbatim (i.e., accept any input) but formats the text nicely?
- 13,297
-
I didn't find a way to use the verbatim package - instead I tried to cover all special cases by unrolling all LaTeX commands and all "special" characters. – Sep 20 '10 at 14:02
5 Answers
The listings package provides you with a verbatim environment which can break lines:
\begin{lstlisting}[breaklines]
Long user text
\end{lstlisting}
If the user text is in an external file you can also use:
\lstinputlisting[breaklines]{filename}
- 3
- 262,582
-
2See also the answer at http://tex.stackexchange.com/questions/11973/verbatim-environment-and-line-breaking for an example of adjusting the formatting of the
lstlistingenvironment. – Scott Morrison Feb 26 '13 at 23:05 -
This works better for me that the second option spverbatim, which breaks the long text only after the page span and hence some text is not seen... – Jozef Janočko Aug 10 '18 at 15:29
-
-
I'm writing in italian and UTF-8 issues of this package are really difficult to manage – massi Apr 18 '23 at 16:59
\usepackage{spverbatim}
...
\begin{spverbatim}
This is a very long line.1
\end{spverbatim}
The spverbatim package enables LaTEX to break lines at spaces within verbatim
text.
- 262,582
-
@MartinScharrer I am trying to create my own environments with this package. See http://tex.stackexchange.com/questions/309042/how-can-i-redefine-verbatim-to-wrap-lines-add-break-before-or-after-spaces – Jonathan Komar May 11 '16 at 13:36
The fancyvrb package in combination with the fvextra package allows you to use linebreaks in the Verbatim environment by specifying the breaklines=true option:
\documentclass{article}
\usepackage{fancyvrb}
\usepackage{fvextra}
\begin{document}
The following is printed verbatim, but with line breaks:
\begin{Verbatim}[breaklines=true]
This is a very long line that will need to be broken into pieces otherwise it will run into and out of the margins
\end{Verbatim}
Alternatively, line breaks can be put anywhere, not just at white space:
\begin{Verbatim}[breaklines=true, breakanywhere=true]
Thisisaverylonglinethatwillneedtobebrokenintopiecesotherwiseitwillrunintoandoutofthemargins
\end{Verbatim}
\end{document}
- 1,200
-
1As mentioned in the documentation, it's also possible to set this option globally with
\fvset{breaklines=true}– user202729 Jul 17 '22 at 02:05 -
Not working in overleaf: `fvextra.sty not found.' or 'Missing number, treated as zero.' if trying to skip fvextra – massi Apr 18 '23 at 17:01
Depending on what you exactly need, this might be sufficient: we reset all special characters to catcode other, in particular \, { and } lose their meaning. This will make it pretty hard to inject LaTeX code.
\documentclass[12pt]{article}
\usepackage[T1]{fontenc}
\makeatletter
\newcommand\saferead[1]{%
\bgroup
\let\do\@makeother
\dospecials\catcode`\ 10
\input{#1}%
\egroup
}
\makeatother
\begin{document}
And to show it, we input ourselves: \saferead{test.tex}
\end{document}
- 9,830
You can use \def\@xobeysp{ } in the preamble of your document to make all spaces regular (breaking) spaces. This will not hyphenate or break ultra-long words, though.
To format the text nicely, you can use the Verbatim environment, in the fancyvrb package:
\begin{Verbatim}[formatcom=\sffamily]
Hello world hello world hello world
Test
\end{Verbatim}
Will print the text in a Sans-seriF font family.
Though (I'm sorry!) the \def\@xobeysp{ } won't work with the Verbatim environment... So you must choose between the two.
- 197
