# Most efficient method to strip all of the LaTeX code from a document?

Can some people please give me some suggestions for the most efficient method to strip all of the LaTeX code from a document?

The best method that comes to my mind, which I have no clue about how to do, is to use some sort of a latexmk_flat_file command that generates a flat text file (without code) instead of a *.pdf.

Running an optical character recognition on the *.pdf will also result in lots of errors and require a substantial amount of manual clean up.

Blocking and copying the resultant *.pdf file gives unwanted line breaks and doesn't normally permit select all text spanning multiple pages.

I used a trial version of Tex2Word by Chikrii, but it was unable to properly handle the type of LaTeX business letter that I am currently using.

catdvi appears to have been last updated in the year 2002, and the kpathsea library presently used by TexLive for Mac/OSX does not have what is required to install the universal distribution of catdvi-0.14 -- i.e., lkpathsea is missing (and perhaps others).

I would like to keep the tabs, spaces, and original line endings.

This is a task that will need to be completed by me several times each month.

With respect to the working draft perl script written by cmhughes, these are the most common codes (modified for the perl script) that are contained within my LaTeX documents:

s/\\begin{.*?}($.*?$)?({.*?})?//g;
s/\\end{.*?}//g;
s/\\hspace\*{.*?}//g;
s/\\vspace\*{.*?}//g;
s/\\tab //g;
s/\~\\\\//g;
s/\\\>//g;
s/\\\=//g;
s/\\textit{//g;
s/\\newpage//g;
s/\{\\bf \\underline{//g;
s/\{\\bsi{//g;
s/\\uuline{//g;
s/\\underline{//g;
s/\}//g;
s/\\//g;
s/\~//g;

• detex + sed; or pdftotext?
– jon
Mar 14, 2013 at 3:00
• Yes, essentially, just export whatever would normally print out on the paper of the *.pdf file. Adobe Acrobat Pro has a save-as option that gets most of it correct, but there are still a few errors that need to be manually cleaned. I will need to do some reading to see if Jon's suggestion would do the trick -- this is the first I've ever heard of them. Mar 14, 2013 at 3:08
• I'm using LaTeX to write business letters, legal pleadings, and invoices to the clients. Mar 14, 2013 at 3:12
• I'd definitely start with pdftotext. You won't run into OCR problems when you have a "regular" non-scanned PDF like pdftex and friends produce. Mar 14, 2013 at 12:06
• Thank you both for the suggestions of pdftotext and/or detex+sed. I am interested in learning more about those options and will do some reading about them in the next few days. Mar 15, 2013 at 5:28

Here's a little perl script that might get you started. You can use it as

 perl removelatexcode.pl myfile.tex myfile1.tex


and can call it with as many files as you like (or you could pipe into it too).

It does the following:

• copies your input file, myfile.tex to myfile.tex.bak just in case something goes wrong
• loops through each line in the file, and only starts working once it hits \begin{document}
• once it is in the main document, it matches patterns such as \begin{<myenvironmentname>}, \end{environmentname}, \<name of command> you can add to it as you see fit.

The way the code stands it won't overwrite the original file. Once you're happy with it, and have tested it to your liking, feel free to go ahead and use the file as

 perl removelatexcode.pl -o myfile.tex


which will overwrite myfile.tex.

Always be careful when using scripts like this- there was no malicious intent here, but, you should test it thoroughly before using it on live files.

If there are some commands for which you wish to keep the argument, for example, \underline{keep this argument} then simply populate

my %keeparguments=("textit"=>1,
"underline"=>1,
);


with the appropriate commands.

removelatexcode.pl

#!/usr/bin/perl

use strict;
use warnings;
use File::Copy;
use Getopt::Std;

# get the options
my %options=();
getopts("o", \%options);

my $inpreamble=1; # switch for in the preamble or not my$filename;
my @lines=();     # @lines: stores the new lines without commands

# commands for which we want to keep the arguments- populate
# as necessary
my %keeparguments=("textit"=>1,
"underline"=>1,
);

while (@ARGV)
{
# get filename from arguments
$filename = shift @ARGV; # open the file open(INPUTFILE,$filename) or die "Can't open $filename"; # reset the preamble switch$inpreamble=1;

# reset the lines array
@lines=();

# loop through the lines in the INPUT file
while(<INPUTFILE>)
{
# check that the document has begun
if($_ =~ m/\\begin{document.*/) {$inpreamble=0;
}
# ignore the preamble, and make string substitutions in
# the main document
if(!$inpreamble) { # remove \begin{<stuff>}[<optional arguments>] s/\\begin{.*?}($.*?$)?({.*?})?//g; # remove \end{<stuff>} s/\\end{.*?}//g; # remove \<commandname>{with argument} while ($_ =~ m/\\(.*?){.*?}/)
{
if($keeparguments{$1})
{
s/\\.*?{(.*?)}/$1/; } else { s/\\.*?{.*?}//; } } # print the current line (if we're not overwritting the current file) print$_ if(!$options{o}); push(@lines,$_);
}
}

# close the file
close(INPUTFILE);

# if we want to over write the current file
if ($options{o}) { # make a backup of each file my$backupfile= "$filename.bak"; copy($filename,$backupfile); # reopen the input file to overwrite it open(INPUTFILE,">",$filename) or die "Can't open $filename"; print INPUTFILE @lines; close(INPUTFILE); # output to terminal print "Backed up original file to$filename.bak\n";
print "Overwritten original file without commands";
}
}

exit


Here's a little test case:

myfile.tex

\documentclass{article}
% in the preamble
% in the preamble
% in the preamble
\begin{document}

\begin{myenvironment}
text text text text text text text text text text
text text text text text text text text text text
text text text text text text text text text text
text text text text text text text text text text
\end{myenvironment}

\mycommand{argument} more text after it \anothercommand{another argument}

\textit{keep this argument} more text after it \anothercommand{another argument} yet more text

\anothercommand{another argument} yet more text \textit{keep this argument} more text after it

\begin{anotherenvironment}[optional arguments] could have text here
other other other other other other other other other other
other other other other other other other other other other
other other other other other other other other other other
other other other other other other other other other other
\end{anotherenvironment}

\begin{anotherenvironment}[optional arguments]{mandatory args} could have text here
another another another another another another
another another another another another another
another another another another another another
another another another another another another
\end{anotherenvironment} can have text here

\end{document}


and the output of

perl removelatexcode.pl myfile.tex


Output

  text text text text text text text text text text
text text text text text text text text text text
text text text text text text text text text text
text text text text text text text text text text

more text after it

keep this argument more text after it  yet more text

yet more text keep this argument more text after it

could have text here
other other other other other other other other other other
other other other other other other other other other other
other other other other other other other other other other
other other other other other other other other other other

could have text here
another another another another another another
another another another another another another
another another another another another another
another another another another another another
can have text here


A few words about regexp

You'll notice the script uses lines such as

s/\\begin{.*?}($.*?$)?({.*?})?//g;


This matches

• \begin{<environmentname>}
• \begin{<environmentname>}[<optional arguments>]
• \begin{<environmentname>}[<optional arguments>]{<mandatory arguments>}

but it does so in a non-greedy way. The .*? makes it no-greedy, and the ? after the grouping () make them optional. If these matches were greedy (which they would be without the ?) then you would get a lot of potentially unwanted results.

• Thank you so very very much for the perl script! I'm looking forward to trying it out and customizing it over the next few days (as time permits). I will be adding to the search parameters all of the codes for which I've created Sublime snippets, and a few other codes that are most common in my documents (but for which no snippet presently exists). Mar 14, 2013 at 4:55
• @lawlist that's great, I'm glad it helped. I've added a simple test case just for example :) Mar 15, 2013 at 1:44
• Thank you for the test run example. I have a few minutes tonight to give it a try, and I'll report back. Mar 15, 2013 at 2:38
• s/\\.*?(.*?)?{.*?}//g; appears to be a little too aggressive: (1) It overrides the switch (when set) to preserve the preamble, and causes the majority of the preamble to be deleted; (2) When using \textit{anything} or \uuline{anything} or \underline{anything}, the stuff between the wavy brackets is deleted; and, anything prior to the code all the way to the beginning of the line is also deleted. Perhaps the following example should should be specified by me individually in the script: {\bf \underline{anything}} -- the words in brackets disappears and only {} remains. Mar 15, 2013 at 5:00
• I'm not sure if this is correct, but I am setting the switch to preserve the preamble at line 24 (changing it to a zero, instead of a 1). It appears to work correctly when switching line 24 -- I tried switching only line 7, but that doesn't seem to have any effect. Thank you again for taking the time to put together this wonderful script. It already works sufficiently well by not using s/\\.*?(.*?)?{.*?}//g;, but then I would need to specify each possibility one at a time. If it's not too much trouble, perhaps you could please suggest a modification of that particular line of code. Mar 15, 2013 at 5:07

Pandoc accepts many different input formats including LaTeX and can produce a variety of outputs including plain text. To try Pandoc online, visit the Try pandoc! site.

As stated on the Pandoc website:

If you need to convert files from one markup format into another, pandoc is your swiss-army knife. Pandoc can convert documents in markdown, reStructuredText, textile, HTML, DocBook, LaTeX, or MediaWiki markup to

• HTML formats: XHTML, HTML5, and HTML slide shows using Slidy, Slideous, S5, or DZSlides.
• Word processor formats: Microsoft Word docx, OpenOffice/LibreOffice ODT, OpenDocument XML
• Ebooks: EPUB version 2 or 3, FictionBook2
• Documentation formats: DocBook, GNU TexInfo, Groff man pages
• TeX formats: LaTeX, ConTeXt, LaTeX Beamer slides
• PDF via LaTeX
• Lightweight markup formats: Markdown, reStructuredText, AsciiDoc, MediaWiki markup, Emacs Org-Mode, Textile
• Welcome to TeX.sx! If you could post a sample of input and output with the commands used, that would be helpful. Mar 14, 2013 at 15:25
• Thank you very much for the suggestion. I would like to research and try out this particular option. There are just so many hours in the day; however, so it may be a few days before I have the opportunity to get up to speed. Mar 15, 2013 at 5:23
• @AndrewSwann: if you're looking for samples -- the Pandoc demo page has lots of them. You can see the source files taken, the commandlines used and see the results generated from both... Nov 13, 2013 at 16:16
• I just used pandoc for the first time after finding this thread. The installation was painless and it converted my 100+ page thesis from latex to word with near perfection. The only additional thing I wish it handled is citations.
– Dsel
Aug 30, 2017 at 16:40
• Pandoc's LaTeX is sketchy at best. Aug 31, 2020 at 23:27

In the spirit of the Pandoc answer, I'd like to suggest the excellent Org-mode for the Emacs editor. Once you are comfortable with Emacs (which might take a few days, but if you want edit lots of text files efficiently, this is a wise investment), Org-mode is very easy to start with, and contains not only powerful export options (including LaTeX, ODT, HTML, and more), is wholly based on plain text files, and comes with task and time management systems and much more.

Disclaimer: Org-mode is a free tool and I'm not affiliated with it;).

• One note that came to my mind just after posting this: "being comfortable with Emacs" may take more than a few days. Depending (less) on your learning speed and (more) of your notion of "comfortable", it might as well be a few years;). Mar 16, 2013 at 20:49

The command detex (on CTAN fits the bill (included in TeXlive), but it is marked "obsolete" and suggests untex and a few others (bot none included in TeXlive/MikTeX as far as I see).