Thursday, September 19, 2013

htlatex and pdflatex

I'm a big fan of the gnuplot tool that has an output terminal of pdflatex. This produces a pdf figure with an overlay of latex for beautiful results. Similarly, inkscape has an output format with pdf overlayed with latex.

I had a problem with htlatex. I use pdflatex and so everything is a pdf in my figures. My overlayed output from latex, with the file extension 'pdf_tex' uses an import file and then includegraphics at the bottom. htlatex complained it couldn't find the bounding box of my pdf file... which is reasonable enough as htlatex calls pdftex and apprently doesn't support including pdfs.

My solution was to include the following in my preamble. This allows the file to continue to work as previously with 'pdflatex' and when 'htlatex' is called, the convert program is called to generate a eps file.

 \graphicspath{{figures/}{results/}}

% call as:
% htlatex myfile.tex  "" "" "" -enable-write18
%
\makeatletter
\LetLtxMacro\latexincludegraphics\includegraphics
\@ifpackageloaded{tex4ht}
    {\renewcommand{\includegraphics}[2][]{%
        \immediate\write18{s=\Ginput@path; (IFS='{}'; for i in $s; do convert ${i}#2 ${i}#2.eps; done)}%
        \latexincludegraphics[#1]{#2.eps}}}
    {\renewcommand{\includegraphics}[2][]{\latexincludegraphics[#1]{#2}}}
\makeatother

Notes: 
  • you must call htlatex with -enable-write18 so it can execute commands in the shell.
  • you must include your graphics path setup before this gubbins.

No comments: