[Tex/LaTex] How to escape plain text

conversionconvertprogramming-toolstext-only

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…)

(based on Underscores in words (text) and How does one insert a backslash or a tilde (~) into LaTeX? Escape character in LaTeX)

\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?).

Best Answer

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.

enter image description here