I would like a way to convert a document from HTML to LaTeX, on a Windows platform.
A main motivation of mine is for ways to display books from Project Gutenberg. such as, Wells' The Invisible Man.
What is my best option?
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It only takes a minute to sign up.
Sign up to join this communityI would like a way to convert a document from HTML to LaTeX, on a Windows platform.
A main motivation of mine is for ways to display books from Project Gutenberg. such as, Wells' The Invisible Man.
What is my best option?
What your best option would be depends on a lot on what your needs are. Are you only trying to import the structure, or exact look, or what? How important is it that the resulting document really be done properly?
Anyway, here are a number of things to try.
AbiWord: an open source word processor that can import HTML or similar formats and export LaTeX. (Be sure to install the extra export plugins when installing; the default install doesn't include a LaTeX export, but it can easily be chosen.)
Writer2LaTeX: An openoffice plugin for exporting to LaTeX; Open office supports HTML import of course (Though W2L can handle .odt to .tex even without Open Office installed; but then converting .html to .odt might be trickier.)
rtf2latex2e: as its name implies, converts RTF to LaTeX; so you'd need some way to convert HTML to RTF (though that's relatively easy, can be done with most any word processor).
pandoc: Haskell program for converting between various mark-up languages, including HTML and LaTeX
html2latex: Perl script for such conversions (I've never tried it but plan on doing so soon)
htmltolatex Java program along similar lines (Again, I haven't tried it.)
Even with all those options, however, personally, if it was something I truly cared about doing right, simply transferring over the plain text and redoing everything manually would still be my solution of choice. The above are just quick fixes for a document of relatively little importance, or when having it in LaTeX in addition to HTML is just a matter of convenience.
If the document is XHTML (rather than HTML), then it can be processed directly in ConTeXt. See http://dl.contextgarden.net/myway/tas/xhtml.pdf for a tutorial and http://dl.contextgarden.net/myway/tas/ for the sample files.
Short answer:
pandoc --standalone index.html --output index.tex
I'm not thrilled by the results but it's a start.
(Sorry I know this isn't a windows-friendly result but this question appears high in Google search results for a more generic search too)
The following sed
script is meant to convert HTML to (La)TeX (source):
1i\
\\documentstyle{article}
1i\
\\begin{document}
$a\
\\end{document}
# Too bad there's no way to make sed ignore case!
/<[Xx][Mm][Pp]>/,/<.[Xx][Mm][Pp]>/b lit
/<.[Xx][Mm][Pp]>/b lit
/<[Ll][Ii][Ss][Tt][Ii][Nn][Gg]>/,/<.[Ll][Ii][Ss][Tt][Ii][Nn][Gg]>/b lit
/<.[Ll][Ii][Ss][Tt][Ii][Nn][Gg]>/b lit
/<[Pp][Rr][Ee]>/,/<.[Pp][Rr][Ee]>/b pre
/<.[Pp][Rr][Ee]>/b pre
# Stuff to ignore
s?<[Ii][Ss][Ii][Nn][Dd][Ee][Xx]>??
s?</[Aa][Dd][Dd][Rr][Ee][Ss][Ss]>??g
s?<[Nn][Ee][Xx][Tt][Ii][Dd][^>]*>??g
# character set translations for LaTex special chars
s?>.?>?g
s?<.?<?g
s?\\?\\backslash ?g
s?{?\\{?g
s?}?\\}?g
s?%?\\%?g
s?\$?\\$?g
s?&?\\&?g
s?#?\\#?g
s?_?\\_?g
s?~?\\~?g
s?\^?\\^?g
# Paragraph borders
s?<[Pp]>?\\par ?g
s?</[Pp]>??g
# Headings
s?<[Tt][Ii][Tt][Ll][Ee]>\([^<]*\)</[Tt][Ii][Tt][Ll][Ee]>?\\section*{\1}?g
s?<[Hh]n>?\\part{?g
s?</[Hh]n>?}?g
s?<[Hh]1>?\\section*{?g
s?</[Hh][0-9]>?}?g
s?<[Hh]2>?\\subsection*{?g
s?<[Hh]3>?\\subsubsection*{?g
s?<[Hh]4>?\\subsubsection*{?g
s?<[Hh]5>?\\paragraph{?g
s?<[Hh]6>?\\subparagraph{?g
# UL is itemize
s?<[Uu][Ll]>?\\begin{itemize}?g
s?</[Uu][Ll]>?\\end{itemize}?g
s?<[Ll][Ii]>?\\item ?g
# DL is description
s?<[Dd][Ll]>?\\begin{description}?g
s?</[Dd][Ll]>?\\end{description}?g
# closing delimiter for DT is first < or end of line which ever comes first NO
#s?<[Dd][Tt]>\([^<]*\)<?\\item[\1]<?g
#s?<[Dd][Tt]>\([^<]*\)$?\\item[\1]?g
#s?<[Dd][Dd]>??g
s?<[Dd][Tt]>?\\item[<?g
s?<[Dd][Dd]>?]?g
# Other common SGML markup. this is ad-hoc
s?<sec[ab]>??
s?</sec[ab]>??g
# Italics
s?<it>\([^<]*\)</it>?{\\it \1 }?g
# Get rid of Anchors
:pre
s?<[Aa][^>]*>??g
s?</[Aa]>??g
# This is a subroutine in sed, in case you are not a sed guru
: lit
s?<[Xx][Mm][Pp]>?\\begin{verbatim}?g
s?</[Xx][Mm][Pp]>?\\end{verbatim}?
s?<[Ll][Ii][Ss][Tt][Ii][Nn][Gg]>?\\begin{verbatim}?g
s?</[Ll][Ii][Ss][Tt][Ii][Nn][Gg]>?\\end{verbatim}?
There is also HTML to TeX, written in C, that produces LaTeX output.
This is why Project Gutenberg should've used sensible markup from the beginning. Fortunately, things are getting marked up now, so one can use:
to convert to Text Encoding Initiative (TEI) and then use Dima's answer at: How can I efficiently convert TEI documents into LaTeX?
For a page from a typical website, the best way is probably to simply:
verbatim
) any text with special characters in it(Of course, if you actually have a well-structured page, which e.g. uses H1 for the document title and properly nests H2- and H3-headed sections, it might be worth trying a converter program. You'll still need to add markup for the things that HTML does not provide markup for, of course.)