# How can I escape plain text?

How, given string of plain text one may produce equivalent latex code? For example

red fox
sauté
zażółć gęślą jaźń
\newline is a new line in LaTeX
Samp_Dist_Corr


would produce something like

red fox\newline
saut\'e\newline
za\.z\'o\l\'c g\c{e}\'sl\c{a} ja\'z\'n\newline
\textbackslash{}newline is a new line in LaTeX\newline
\verb|Samp_Dist_Corr|


(example output above is poor - ę and ą are not converted properly, Samp_Dist_Corr is displayed in other style than rest of the text...)

\obeylines seems to be enough to handle newlines (Insert a new line without \newline command) (though replacing \n character and \r\n combination with \newline text is the simplest part).

\verbatim is not a solution for escaping as I want LaTeX to manage linebreaks (and according to https://tex.stackexchange.com/a/153595/69392 with \verbatim text wrap is disabled).

https://stackoverflow.com/questions/1696997/convert-plain-text-to-latex-code-programmatically discusses this problem and mentions additional pitfalls, but no solution is mentioned

Overall, I would strongly prefer to use any existing library/script rather than create a new one and discover special cases over and over again.

I want to note that I would be happy about any script/library, in any programming language.

To avoid XY problem: I have multiple txt files with cooking recipes that I want to convert into a good looking pdf file. I am able to automatically separate each text file into title/ingredient list/cooking instructions/time necessary for preparing etc. So I want to produces a TeX file that is a merge of all reciped with necessary TeX formatting. Later I will produce pdf file by compiling text using pdflatex.

Situation is a bit complicated as recipes are in Polish so żółćęśąźńŻÓŁĆĘŚĄŹŃ are expected, also é and other foreign letters may appear in specialized words (like sauté) or in mentions of original recipe title - so I would prefer general solution over stuffing babel directive with massive list of languages (though maybe that is a preferable solution over escaping?).

Here's an idea using fancyvrb (with the fvextra extensions).

\documentclass{article}
\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}
\usepackage{showframe} %to show boundaries, remove this line for normal output

\usepackage{fvextra}
\DefineVerbatimEnvironment{mateusz}
{Verbatim}
{fontfamily=\rmdefault,breaklines,breaksymbolleft={}}

\begin{document}

\begin{mateusz}
red fox
sauté
zażółć gęślą jaźń
\newline is a new line in LaTeX
Samp_Dist_Corr
whatever whatever whatever whatever whatever whatever whatever whatever whatever whatever whatever
\end{mateusz}

\end{document}


I used showframe just to show the boundaries of the text block.

• Small note for people who use Ubuntu and simply installed texlive-full - hack to get fancyvrb is to view github.com/gpoore/fvextra/blob/master/fvextra/fvextra.sty in raw mode and download it to folder where .tex file is compiled. Dec 20, 2016 at 7:32
• And according to tex.stackexchange.com/questions/1137/… placing it in kpsewhich -var-value=TEXMFHOME would be even better. Dec 22, 2016 at 9:54
• Footnote to above: place .sty file in /tex/latex/ subdirectory of TEXMFHOME! Dec 22, 2016 at 10:33
• @egreg Like ur hat.
– user10274
Dec 23, 2016 at 6:36
• Unfortunately it fails for section titles. I created tex.stackexchange.com/questions/357425/… to cover this. Mar 8, 2017 at 8:24

\verbatim is not a solution for escaping as I want LaTeX to manage linebreaks

The package spverbatim also allow line breaks at spaces. It work with both pdflatex and xelatex with the appropriate preamble. For example with xelatex:

\documentclass[a4paper,twocolumn]{article}
\usepackage{spverbatim}
\usepackage{fontspec} % Do not use with pdflatex
% \usepackage[T1]{fontenc} % with pdflatex only
% \usepackage[utf8]{inputenc} % with pdflatex only
\begin{document}
\begin{spverbatim}
Long text where it is allowed line breaks at the spaces.
red fox
sauté
zażółć gęślą jaźń
\newline is a new line in LaTeX
Samp_Dist_Corr
So żółćęśąźńŻÓŁĆĘŚĄŹŃ are expected.
\end{spverbatim}
\end{document}


so żółćęśąźńŻÓŁĆĘŚĄŹŃ are expected, also é and other foreign letters

With utf8 option in inputenc package and T1 option in fontenc you will have all the characters that you have showed. If there are others, you may need additional solutions. For instance, often I should use € and µ symbols, but the above is not enough with the default font. There are differents solutions for this cases, for example, the textcomp package, or a font package as libertine, but also others packages with support for euro or greek symbols.

If you have problems with other symbols, consider switch to xelatex or lualatex with the fontspec package. Even then, you may need change the font to have the more odd glyphs. In some cases, even using fonts that are not in CTAN. For example, many (all?) fonts in CTAN do not have a glyph for the unicode mouse emoji (🐁), but you can install a special font in your OS like OpenSansEmoji and use it with xelatex using \setsansfont{OpenSansEmoji} in the preamble and \textsf{🐁} in the body of the document (afaik there are only the sans version, therefore you cannot use 🐁 within verbatim environment except with \setmonofont{OpenSansEmoji})

• Note that it is useless for pdflatex (Fatal fontspec error: "cannot-use-pdftex" The fontspec package requires either XeTeX or LuaTeX to function.) - and I am using LaTeX because I want to generate pdf diocuments. Mar 8, 2017 at 7:37
• @MateuszKonieczny That is what it says the first sentence of the last paragraph (although indirectly by omission).
– Fran
Mar 8, 2017 at 8:40