# Conversion of tex to latex

Quite surprisingly for me, I did not find valuable information on how to convert plain TeX files to LaTeX documents. There is a TeX.SX question, but it was answered with a particular solution.

I have a larger number of simple TeX files with equations which I would like to convert to LaTeX articles. The need arises because I want to merge the documents with others which are based on LaTeX. The files do not contain any images and no complicated layouts.

Is there a simple but stable conversion tool somewhere out there which helps me to achieve this?

There seems to be a tool called tex2latex but I only found a windows binary. I work on Ubuntu and my LaTeX distribution is TeX Live.

• As TeX is Turing-complete, your input could in principle contain anything: that's a challenge for a general tool! We probably need some example of how your input typically looks to make helpful suggestions for your case. – Joseph Wright Sep 29 '14 at 10:03
• Did you take a look on plain.sty? It allows you to write \begin{plain}\input{plain-doc}\end{plain}. Could be handy if you really just want to include some equations or alike. – LaRiFaRi Sep 29 '14 at 10:09
• @wipet Not necessarily; \frac is defined as {\begingroup#1\endgroup\over#2} for a reason. – egreg Sep 29 '14 at 10:18
• @highsciguy I don't think a tool is possible in general (to parse arbitrary definitions and recode them in a more latex-like way) it may be possible to spot simple idioms such as {a \over b} and change to \frac but in general this won't be possible and for the other cases really the safe thing to do is leave them as they are. – David Carlisle Sep 29 '14 at 11:31
• @highsciguy honestly, I wouldn't do it. regexp replacements in general will destroy valid tex input. For specific documents with a naturally limited range of idioms it may be worth just doing a query-replace "by hand" in an editor but a general scripted solution has a high probability of breaking documents for essentially no functional gain (since it is easy to make the plain tex markup work in latex, which is what plain package does) – David Carlisle Sep 29 '14 at 12:09

\documentclass{article}
\usepackage{plain}
\begin{document}
\begin{plain}
\input{texfile}
\end{plain}
\end{document}

• couldn't be simpler could it? of course a DPC package :-) – Frank Mittelbach Sep 29 '14 at 16:35
• I tried a bit and found that I can apparently use also latex code in the plain environment. Is there a restriction to this? I am asking because the goal is to merge rather than to concatenate tex and latex files. This means that I want e.g. to take a tex equation and combine it with a latex equation. – highsciguy Sep 30 '14 at 10:36
• Generally packages only add functionality. There might be a problem if a macro name means one thing in plain TeX and another in LaTeX. Note that environments and counters are actually implemented using macros and counts. For example, \equation \endequation and \c@page – John Kormylo Sep 30 '14 at 15:57

The difficulty, at least with using regexes on a non regular language, is perhaps best seen via an example. Given a command \eqalignno, one could perhaps try a multiline match with, say, awk: /\\eqalignno\{/, /^ +\}/ { do_something } (match for a range, starting with the pattern inside the first //, and continue on until finding the second pattern. But already you can see that it relies on the source file being formatted in a specific way; the latter pattern expects the closing brace to be on a line of its own (with possible preceding whitespace). The reason I started to answer was that I started to work on such regexes for this question, and while I got the matcher working on one of my files, in the very second one I had used instead: \eqalign{\n ...\cr ...\n }$$\n. Well okay, you just include a test for the presence of those closing display math dollars, right? So the latter pattern would become /^ +\}(\\)?/. But wait, in the next file I had used ...\cr}$$\n instead. And yet in another file, ...\n }$...\n! This reminded me of a previous Stack Overflow question, and so would like to close with the words: The \center cannot hold it is too late. • I think for this kind of problem I have the solution (I use it routinely to parse latex commands). It is balanced pattern matching. In perl: my$np; $np = qr{ \{ (?: (?> [^\{\}]+ ) | (??{$np }) )* \} }x;. However that is complicated by the fact that not all tex command have the form \eqalignno{..{}..}..{..} For example \over is different. – highsciguy Sep 29 '14 at 18:29
• @highsciguy: Yeah, and for the above you'd need to differentiate between \{/\} and {/}, etc. etc. – morbusg Sep 30 '14 at 5:43
• As long as \{ and \}, this would still be easy. If they are unmatched? – highsciguy Oct 2 '14 at 19:20
• @highsciguy: actually, yeah, I think you're right. So at the moment my answer is misleading; I'll update it once I get the hang of recursive named groups. – morbusg Oct 2 '14 at 20:56
• @highsciguy: I'm sorry I just can't get sub capture groups to work; this is what I've tried in ruby: @texregexp = /\$$?<macro>[[:alpha:]]+\s*(?<args>(?!\$$\{(\g<macro>|[^{}])*\}))/m (testing with a_string.scan @texregexp) – morbusg Oct 8 '14 at 8:48