15

I want an environment in which LaTeX typesets the text just like it otherwise would, but without accepting any commands whatsoever while within this environment. None of the answers to this question solves the problem. Neither does fancyvrb, nor the verbatim packages.

I need this functionality, as I'm composing a LaTeX document from several plain text sources, with text written by authors with no idea of TeX or LaTeX. So the environment should treat everything as normal text. It should break lines, hyphenate words and just do what LaTeX does to normal text.

Do anyone have an idea on how to solve this problem?

EDIT:

An example would be someone writing this, in the plain text input field:

Plain text input

That will result in the following PDF output:

enter image description here

All is still fine, but then someone comes up with the fine idea of writing "\textbf{}" somewhere in the text. It could really be anything, like "%" or "$" or "_", but I think you get the idea. The text input now looks like:

enter image description here

And the PDF output looks like:

enter image description here

But I want it to look like:

enter image description here

So, I'm looking for an environment like:

enter image description here

which still typesets the text like it would any other text, but ignores all special characters and commands. I really, really don't want to write a parser which does all of the magic if I can avoid it...

EDIT 2:

Using the method devised in the answer from David Carlisle:

enter image description here

results in this PDF:

enter image description here

So not quite there yet.

EDIT 3:

Mea culpa - We absolutely are there. I've forgotten to add the "\usepackage[T1]{fontenc}". I can't make it fail, so I'm happy. Thanks David!

  • 2
    Can you then not use \input{foo} and put the plain text inside foo.tex? You can't really have it both ways. It is either Verbatim or not ;) – Nasser Jul 10 '15 at 16:17
  • Can you provide an example of such a text and the expected output? – Werner Jul 10 '15 at 16:24
  • @Nasser why can you not have it both ways? – David Carlisle Jul 10 '15 at 16:34
  • @DavidCarlisle because when I googled for a Latex package called \usepackage{almost_verbatim} I did not find any :) – Nasser Jul 10 '15 at 16:35
  • It makes the site very confusing if you edit the answer into the question. You only get " for \ as you don't copy the code correctly, you need \usepackage[T1]{fontenc} – David Carlisle Jul 10 '15 at 18:25
17

enter image description here

\documentclass{article}
\usepackage[T1]{fontenc}

\setlength\textwidth{20em}


\begingroup
\makeatletter
\gdef\plaintext{\let\do\@makeother\dospecials\catcode32=10\relax\xplaintext}
\catcode`\!=0
!catcode`\\=12
!catcode`{=12
!catcode`}=12
!catcode`(=1
!catcode`)=2
!long!gdef!xplaintext#1\end{plaintext}(%
#1!end(plaintext))
!endgroup



\begin{document}


\begin{plaintext}

this % text has ^ & _ but seems to work alright, hyphenation 
or extremely longish words seems to work OK.

Blank lines do form a paragraph but \this is not an undefined
command and \includegraphics{this is not a picture}

\end{plaintext}

\end{document}
  • Could this code be made into a package to make it easy to use for users? as in \usepackage{plaintext} and have it be part of TexLive distribution? This would be very useful. thanks. – Nasser Jul 10 '15 at 16:43
  • @Nasser I think you'll find the licence is such that you may do that, yes:-) – David Carlisle Jul 10 '15 at 18:58
  • Does this still work if I ask a question like this in the environment!?!?!???!?!?!? (Point being: can you still have exclamation marks in there?) – Sean Allred Jul 10 '15 at 20:11
  • @SeanAllred you should have more faith:-) – David Carlisle Jul 10 '15 at 20:20
  • The only place where this don't work is in a title, like : \chapter{A \begin{plaintext} \textbf{short} \end{plaintext} example}. But there's properly a good reason for that. – colding Jul 11 '15 at 10:27
6

This approach uses \detokenize, but wraps it in a paragraph parser, which will format lines and indeed paragraphs in a verbatim-like manner. EDITED to incorporate a trap for the % character, taking a nod from David's approach. However, there is (at least) one area where it will break: unmatched braces {}.

However, this limitation may or may not pose an insurmountable problem for you.

\documentclass{article}
\usepackage[T1]{fontenc}
\newcommand\verbpars{\bgroup\catcode`\%=12 \verbparsProper}
\newcommand\verbparsProper[1]{\verbparhelp#1\par\relax\relax\egroup}
\long\def\verbparhelp#1\par#2\relax{%
  \detokenize{#1}\ifx\relax#2\else\par\verbparhelp#2\relax\fi}
\begin{document}
\verbpars{%
this is a test $#@\\&!@~`_<>|\ this is a test $#@\\&!@~` this is a test $#@\\&!@~`
  this is a test $#@\\&!@~` this is a test $#@\\&!@~` this is a test $#@\\&!@~`

and now I have another test.} Regular \LaTeX{} continued in same par.

\verbpars{I have succeeded in making it work with an unquoted 
  percent sign % but it will still break with unmatched braces{}.  

But otherwise it may get you what you need.}

% This should not be seen
\end{document}

enter image description here

  • 1
    probably safest to just make % catcode 12 locally as in my answer, otherwise comments in included files will be tricky:-) – David Carlisle Jul 10 '15 at 16:44
  • @DavidCarlisle Fixed – Steven B. Segletes Jul 10 '15 at 16:46

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.