3

I would like to add metadata (via meta tags and JSON-LD) to my tex4ht output, and for this leverage the title tag content in some places.

Using the following MWE:

\documentclass{scrbook}

\title{Some Book}
\subtitle{A novel}
\author{Some Author}

\makeatletter
\let\mytitle\@title
\let\mysubtitle\@subtitle
\let\myauthor\@author
\let\mydate\@date
\makeatother

\begin{document}

\chapter{Introduction}
\chapter{A great beginning}

\end{document}

with the following make4ht.cfg:

\Preamble{xhtml,ext=html,charset="utf-8"}
\Configure{TITLE+}{\mytitle{}: \mysubtitle{} | \myauthor{}}
\Configure{chapterTITLE+}{\chaptername{} \thechapter{}: #1 | \mytitle{}: \mysubtitle{} | \myauthor{}}
\Configure{likechapterTITLE+}{#1 | \mytitle{}: \mysubtitle{} | \myauthor{}}
\Configure{@HEAD}{\HCode{\Hnewline<meta property="og:title" content="\LikeRef{TITLE+}" />}}
\begin{document}
\EndPreamble

I get exactly what I want for the title tag of each page, i.e. the book name (title, subtitle, author) if producing a single HTML and the chapter number and title prefixed if splitting by chapters.

Is there a way to leverage this e.g. for og:title or other metadata fields? The indicated example using \LikeRef{TITLE+} does not appear to work (empty output).

For completeness, I am compiling with:

make4ht --format html5 --config make4ht.cfg --utf8 book.tex  "2,sec-filename,sections+"

but I'd also like the "single page version" to work:

make4ht --format html5 --config make4ht.cfg --utf8 book.tex  "1,sec-filename,sections+"

1 Answer 1

3

Try this configuration file:

\Preamble{xhtml,ext=html,charset="utf-8"}
\Tag{mytitle}{\mytitle: \mysubtitle\space| \myauthor}
\Configure{TITLE+}{\LikeRef{mytitle}}
\Configure{chapterTITLE+}{\chaptername{} \thechapter{}: #1 | \mytitle{}: \mysubtitle{} | \myauthor{}}
\Configure{likechapterTITLE+}{#1 | \mytitle{}: \mysubtitle{} | \myauthor{}}
\Configure{@HEAD}{\HCode{\Hnewline<meta property="og:title" content="\LikeRef{mytitle}" />}}
\begin{document}
\EndPreamble

The important change is that we save the custom title to the \Tag{mytitle} property, which can be then read with the \LikeRef{mytitle} command. \LikeRef{TITLE+} doesn't work because \Configure{TITLE+} doesn't save the property using the \Tag command, so it isn't available in \LikeRef. But this property is saved by \maketitle, with \@title as value, which is the reason why I chose a different name.

This is the result:

<head><title>Chapter 1: Introduction — Some Book: A novel — Some Author</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='sample.css' rel='stylesheet' type='text/css' /> 
<meta content='sample.tex' name='src' /> 
 
<meta content='Some Book: A novel | Some Author' property='og:title' /></head>

Edit:

If you want to have the same title and og:title, you can use a DOM filter. Try this file, build.lua:

local domfilter = require "make4ht-domfilter"

local process = domfilter {
  function(dom)
    -- copy current page title from <title> to <meta property="og:title">
    local head = dom:query_selector("head")[1]
    -- find head element for faster processing
    if head then
      local title
      for _, el in ipairs(head:query_selector("title")) do
        title = el:get_text()
      end
      if title then
        for _, meta in ipairs(head:query_selector("meta[property='og:title']")) do
          meta:set_attribute("content", title)
        end
      end
    end
    return dom
  end
}

Make:match("html$", process)

Compile using:

make4ht --format html5 --config make4ht.cfg -e build.lua --utf8 book.tex  "2,sec-filename,sections+"

This is the result:

<head><title>Chapter 1: Introduction — Some Book: A novel — Some Author</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='sample.css' rel='stylesheet' type='text/css' /> 
<meta content='sample.tex' name='src' /> 
 
<meta content='Chapter 1: Introduction — Some Book: A novel — Some Author' property='og:title' /></head>
4
  • Thanks! That's almost there — my question was not clear enough, I think. I was aiming to get exactly the content of the title tag in the property, i.e. when not splitting by sections, with the new config file, I get the correct result. But when splitting by sections (chapters), I'd like the title to be Chapter 1: Introduction — Some Book: A novel — Some Author, i.e. including the chapter title and number. Is that possible, too? Aug 17, 2022 at 22:00
  • 1
    @OliverFreyermuth ah, this seems to be more difficult. You can use DOM filters for this. See my edit.
    – michal.h21
    Aug 18, 2022 at 10:55
  • Wow, this works great, as usual! It works perfectly well. Thanks for the comments in the Lua code, too, this really helps — I am also considering using this approach to generate the JSON-LD now (preferred by some search engines for rich metadata), Lua seems to be well-suited for that. Aug 18, 2022 at 14:38
  • 1
    @OliverFreyermuth sure, you can add a lot of custom elements with Lua filters. see the LuaXML documentation for details about the DOM object, it has lot of useful functions
    – michal.h21
    Aug 18, 2022 at 15:00

You must log in to answer this question.

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