1

I use a command like this to generate HTML files and save them in the directory ./html.

make4ht -x -f html5 -d html doc1.tex

So, for example, this document

\documentclass{article}
\usepackage{graphicx}
\begin{document}
This is a test.
\includegraphics{../Figures/image.png}
\end{document}

when converted with the command above produces the file ./html/doc1.html with contents as follows:

<!DOCTYPE html> 
<html lang='en-US' xml:lang='en-US'> 
<head><title></title> 
<meta charset='utf-8' /> 
<meta content='TeX4ht (https://tug.org/tex4ht/)' name='generator' /> 
<meta content='width=device-width,initial-scale=1' name='viewport' /> 
<link href='doc1.css' rel='stylesheet' type='text/css' /> 
<meta content='doc1.tex' name='src' /> 
</head><body>
<!-- l. 4 --><p class='noindent'>This is a test. <img alt='PIC' src='../Figures/image.png' />
</p>
</body> 
</html>

What I'd like is for the img tag in the HTML file to point to the file image.png in the local directory i.e. ./html, and to have the graphics file copied to ./html/image.png. This would allow me to zip up the ./html directory and share it without having to worry about missing graphics files. Is this possible, and if so, how? I am using Linux (Ubuntu).

  • 2
    How are you specifying the ../Figures directory for the files in the .tex file? With \graphicspath or directly in \includegraphics, and are you using absolute or relative paths? Some sample code might help. This might be possible with supplying options for t4ht, and even if it's not possible through make4ht, it's almost certainly possible with a farily simple script. To be clear you don't want to copy the Figures directory to a subdirectory of html/ but want the image files directly in html/ itself? And what OS? – frabjous Sep 07 '22 at 17:27
  • Good points - I've tried to clarify in the revised question. – Gareth Walker Sep 07 '22 at 20:18

1 Answers1

1

Try this build file:

local mkutils = require "mkutils"
local domfilter = require "make4ht-domfilter"

local process = domfilter{ function(dom) for _, img in ipairs(dom:query_selector("img")) do local src = img:get_attribute("src") if src then -- remove path specification src = src:match("([^/]+)$") img:set_attribute("src", src) end end return dom end }

local function image_copy(path, parameters) -- get image basename local basename = path:match("([^/]+)$") -- if outdir is empty, keep it empty, otherwise add / separator local outdir = parameters.outdir == "" and "" or parameters.outdir .. "/" -- handle trailing // outdir = outdir:gsub("//$","/") local output_file = outdir .. basename mkutils.cp(path, output_file) end

Make:match("png$", function(path, parameters) image_copy(path, parameters) -- prevent further processing of the image return false end)

Make:match("html$", process)

It does two things -- first, it removes file path from <img src="path/image.png">, second, it copies png images to the output directory.

If you want to make directory with all output files, including images, you can use this command:

make4ht -e build.lua -xd html doc1.tex

This is the resulting HTML:

<!--  l. 5  --><p class='noindent'>This is a test. <img alt='PIC' src='image.png' />
</p>

And directory listing of the html dir:

-rw-r--r--. 1 michal michal 4411  7. zář 23.11 image.png
-rw-rw-r--. 1 michal michal 6067  7. zář 23.11 doc1.css
-rw-rw-r--. 1 michal michal  473  7. zář 23.11 doc1.html
michal.h21
  • 50,697
  • This doesn't point to the Figures subdirectory (a specification I think I added after making the original post) but this still addresses the main point (creating a folder which can be zipped up and shared). Thanks for the excellent answer. – Gareth Walker Sep 08 '22 at 10:11
  • @GarethWalker ah, you are right. I've created my code before your edit and didn't inspect it too much, as it was already time to go to sleep. I can add support for subdirectories, if you are interested. – michal.h21 Sep 08 '22 at 10:21
  • no worries - I've removed the requirement for subdirectories in the original post as this part is not so important (the IMG tags and copying the files were the crucial part) – Gareth Walker Sep 08 '22 at 10:38