77

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?

1
  • 2
    If you're mainly looking for higher quality typography than usually offered by html or epub, I'd recommend using Prince XML, which will give you kerning and ligatures and hyphenation (using TeX's algorithm). See princexml.com ; for converting ePub to PDF using Prince, see mobileread.com/forums/showthread.php?t=89689
    – frabjous
    Sep 15, 2010 at 2:55

6 Answers 6

56

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.

0
14

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.

2
  • 3
    tidy will convert HTML to XHTML. Sep 15, 2010 at 9:06
  • 3
    An advantage of Context over Latex as a target format is that Context allows (encourages, even) much more of a CSS/HTML-like separation of style from content. Sep 15, 2010 at 9:07
9

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)

6

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?&gt.?>?g
s?&lt.?<?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.

1
  • You can have BSD or GNU sed ignore case, using the i modifer
    – hd1
    Apr 28, 2016 at 5:09
2

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?

2
-2

For a page from a typical website, the best way is probably to simply:

  1. Paste the text from the page into a blank LaTeX document
  2. Properly escape (or make verbatim) any text with special characters in it
  3. Fill in the rest of the markup

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

0

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .