13

I want to paste from the clipboard a plain text copied from a text editor (line wordpad++, vim, notepad etc), and I want TeX to respect the same line break formatting that the editor has. This works great between most text editors, however if I copy a text to a TeX editor and compile it, then it does not respect the line breaks.

Of course I can put \\ or \linebreak or whatever other typesetting function, but I don't want that. Also simple ASCII symbols like the ampersand must also be escaped (\&) I just want to have the text in my .pdf the same way I have it in my editor, without further editing it in my TeX editor.

Is there any function|style setting that can help me?

Notice I also need word wrap for the quoted text.

5
  • 4
    Welcome to TeX.SE. For simple ASCII text, @IanThompson's answer shows how to use verbtain. If the text is actually some other programming language that you are trying to show then you should also consider the listings package. Commented Feb 23, 2012 at 17:25
  • 1
    This feels like it somehow points to a fundamental difference in TeX with regards to how it handles newlines and paragraphs, only I don't know exactly how to put it... :(
    – jonalv
    Commented Feb 24, 2012 at 10:07
  • You have two contradictory requests: what did you mean when you added "I also need word wrap"?
    – egreg
    Commented Feb 24, 2012 at 11:29
  • I think he wants the newlines that are there but also wants added line wraps whenever they are needed, in addition.
    – jonalv
    Commented Feb 24, 2012 at 12:55
  • Exactly. I keep a lot of notes on text files and sometimes I just need to include/quote from them in a tex document. And I prefer not to do any re-editing of my notes. Without word-wrapping the larger lines go out of paper. In text editors we have word wrapping and don't care about line breaks.
    – grokkaine
    Commented Feb 24, 2012 at 13:45

5 Answers 5

20

The listings package also allows for setting content verbatim. Here's an example, using language=TeX (which should be fine if you have plain text):

enter image description here

\documentclass{article}
\usepackage{listings}% http://ctan.org/pkg/listings
\begin{document}
\begin{lstlisting}[language=TeX]
Lorem ipsum dolor sit amet, consectetur 
adipiscing elit. Fusce pulvinar risus 
eu neque porttitor a ultrices magna 
malesuada. Mauris ullamcorper, elit at 
euismod fermentum, mi velit imperdiet 
est, in auctor orci lorem eu velit. 
Integer venenatis accumsan mollis. Cras 
in eros metus, id euismod ante. Class 
aptent taciti sociosqu ad litora 
torquent per conubia nostra, per inceptos 
himenaeos. Vivamus nisl tortor, eleifend 
at vestibulum sit amet, scelerisque 
a lacus. Aenean tempus nibh eu ante 
fringilla laoreet. Pellentesque et diam 
sem, sed venenatis turpis. Aenean ac 
euismod dui. Vivamus faucibus elit non 
dolor tincidunt non pulvinar felis 
sagittis. Mauris mi massa, malesuada id 
vestibulum et, tincidunt ac nibh. 
Donec in orci eros, eu accumsan odio. 
Curabitur ipsum nunc, dictum vitae 
faucibus non, aliquet quis leo. Nullam 
nisi diam, rhoncus sagittis semper non, 
pharetra et eros. Etiam a metus nulla, non 
egestas leo.
\end{lstlisting}
\end{document}​​​​​​
3
  • 8
    This cannot be unseen… Commented Feb 23, 2012 at 17:59
  • @AndreyVihrov: :) user requests, however foreign and bizarre, can be accommodated to some extent. This is one of them.
    – Werner
    Commented Feb 23, 2012 at 18:06
  • Thanks, how do I ask it to make word wraps? I tried \lstset{breaklines = true} \begin{lstlisting}[language=latex] ..text.. \end{lstlisting} but it doesn't work.
    – grokkaine
    Commented Feb 24, 2012 at 10:08
14

Use the verbatim environment.

\documentclass{article}
\begin{document}
\begin{verbatim}
Do not format this text.
\end{verbatim}
\end{document}

If you need more sophisticated features, take a look at the fancyvrb package.

1
  • Thanks, this is fine, except one single point: it doesn't do line wrap. I forgot to mention that. :( I am re-editing my question to include it.
    – grokkaine
    Commented Feb 24, 2012 at 9:04
14

If you want it very short, try the plain-TeX \obeylines. It should be used in a group, however. Also, TeX starts a new paragraph after each line in obeylines-mode, which might result in indentation. So you probably want to change indentation (third example below).

Try it like this (with default for comparison):

\documentclass{article}
\begin{document}
Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do
eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad
minim veniam, quis nostrud exercitation ullamco laboris nisi ut

{\obeylines %
Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do
eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad
minim veniam, quis nostrud exercitation ullamco laboris nisi ut
}

{\parindent=0pt \obeylines %
Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do
eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad
minim veniam, quis nostrud exercitation ullamco laboris nisi ut
}

\end{document}

Note that it is often undesirable to mix plain-TeX commands as \obeylines with LaTeX. However, occasionally, such solution might fit. Also, escaping is not done for you with this solution.

For more sophisticated inclusion of verbatim text, use the verbatim environment. Note that it changes the default font to typewriter-style monospaced. The fancyvrb package contains even more sophisticated facilities for verbatim text; it also changes the font in its environment by default.

The listing package is intended to typeset program code listing and syntax-highlighting. It can also be used to simply include verbatim text in your document.

2
  • Thanks, this works except one problem: some ASCII characters must still be skipped. '\&' for example. On verbatim I don't have to skip them, but I have no way to specify lines. I installed listings but language = latex is not among the supported languages, so I don't know what to use.
    – grokkaine
    Commented Feb 27, 2012 at 9:06
  • 1
    Try to use listings with no language set, using: \lstset{language={}}. This should help. Also, if you really want LaTeX as language for listings, eg, for syntax highlighting, use \lstset{language=[LaTeX]TeX} as explained in the listings documentation (Table 1).
    – Tobias
    Commented Feb 29, 2012 at 9:30
9

The simple answer is the alltt package. The alltt is a verbatim like environment that preserves all spaces and lines, but normal TeX commands such as font changes are active.

\documentclass{article}
\usepackage{alltt}
\usepackage{color}
\begin{document}
\begin{alltt}\rmfamily
Lorem ipsum dolor sit amet, \textbf{consectetur}
adipiscing elit. Fusce pulvinar risus
eu neque porttitor a ultrices magna
   malesuada. Mauris ullamcorper, elit at
   euismod fermentum, mi velit imperdiet
est, in auctor orci lorem eu velit.

Integer venenatis accumsan \textcolor{red}{mollis}. Cras
in eros metus, id euismod ante.
\end{alltt}
\end{document}

enter image description here

4

I think Tobias provided the solution (vote him up!) but I don't think his example showed it so good. Hence I am providing a second example:

\documentclass{article}
\begin{document}

{\parindent=0pt \obeylines %
First comes a really really really really really really really long line that needs to be line wrapped automagicly by tex
after that comes
multiple short 
ones 
Where the newlines should be obeyed.
}

\end{document}

enter image description here

1
  • 1
    It does not skip all ascii characters though. For example I must do '\&' for all '&'. See my comment to Tobias.
    – grokkaine
    Commented Feb 27, 2012 at 9:08

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .