4

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

0
5
+100

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

5
1

\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:

mwe

\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})

2
  • 1
    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. – reducing activity Mar 8 '17 at 7:37
  • @MateuszKonieczny That is what it says the first sentence of the last paragraph (although indirectly by omission). – Fran Mar 8 '17 at 8:40
0

Since you say you are okay with using external programs to achieve this task, I would recommend wolfram mathematica.

All that you would need to do is open a notebook and change the input type to "plain text". Then copy and paste the text you want converted and save the notebook as a .tex file.

After trying the sample text you provided the only thing it did wrong was indent the first line. Which, is a simple fix.

1
  • I strongly prefer fully automatic solution. – reducing activity Dec 23 '16 at 11:56

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.