1

This answer shows how to use minted in tex4ht.

But options do not seem to be passed through as with pdf.

For example, trying to change the font size as shown in this answer does not work with tex4ht using same code as with pdf. The fontsize remains the same in HTML.

Here is MWE and the commands used. Question is: How to change font size of minted with tex4ht?

\documentclass[12pt]{article}
\usepackage{minted}
\begin{document}

example 1

\begin{minted}{c} int main() { printf("hello, world"); return 0; } \end{minted}

example 2

\begin{minted}[fontsize=\footnotesize]{Text} Methods for first order ODEs: --- Trying classification methods --- trying a quadrature <- quadrature successful \end{minted}

example 3

\begin{minted}[fontsize=\tiny]{Text} Methods for first order ODEs: --- Trying classification methods --- trying a quadrature <- quadrature successful \end{minted}

\end{document}

Compiled using lualatex -shell-escape foo.tex on TL 2021 gives expected output

enter image description here

But with tex4ht compiled using

make4ht --shell-escape -ulm default -c my_cfg.cfg foo.tex "mathjax,htm"

gives the HTML

enter image description here

Which shows the fontsize did not pass through.

The following is the file my_cfg.cfg used in the above command

\Preamble{xhtml,p-width}

%see https://tex.stackexchange.com/questions/554995/creating-better-html-code-with-minted-and-tex4ht \ConfigureEnv{minted}{\NoFonts}{\EndNoFonts}{}{} \begin{document} \EndPreamble

Any workaround to make all minted options work with tex4ht also? if not all options, at least making fontsize work will be useful.

Nasser
  • 20,220
  • you are using a local configuration from the linked answer, did you read what that answer says the configuration does? These command turn off and on handling of font styling. so you are explicitly turning off font handling. Assuming your final document is referencing some css you can easily globally style all the generated code by applying css to the appropriate classes – David Carlisle Mar 13 '22 at 10:39
  • you can easily globally style all the generated code by applying css to the appropriate classes I really do not want to do this. I just wanted to use minted as is. I did not realize this limitation when I read the answer. For now, I will just live with it as is, without the fontsize being smaller in HTML. Not a big issue. It will be great if tex4ht could support minted with all its options. At least the font size option. – Nasser Mar 13 '22 at 10:46
  • I was suggesting using mined ouput as is, bu doesn't your document use any css at all? if it does just add pre{font-size: smaller} and all <pre> get smaller, you don't need to touch mined at all. (or check exactly what class is generated by make4ht to give a more specific rule) – David Carlisle Mar 13 '22 at 10:50
  • minted does support that it's just that you disabled it. You are using a local config the only documented effect of which is to disable font handling and then ask why font commands are ignored? – David Carlisle Mar 13 '22 at 10:52
  • @DavidCarlisle Ok. I will try this. You mean to set the fontsize in Latex itself before calling minted and then set the fontsize back to normal. I will try this. I did not know this would work. Thanks. – Nasser Mar 13 '22 at 10:54
  • No I didn't mean that (alhough it will probably work) I meant what I said, I'd just use a css that syled the document. But more generally I was commenting on your question which is worded as asking about minted behaviour when you are really asking about the behaviour of the config file that you use but don't actually mention here. – David Carlisle Mar 13 '22 at 10:57

1 Answers1

1

You explicitly disabled TeX4ht font processing with ConfigureEnv{minted}{\NoFonts}{\EndNoFonts}. So if you just removed this code, you would get a correct font size for your listings. But the downside is that lot of HTML code will be created. If you want to have a cleaner code, you can use CSS to declare font sizes for particular environments.

Personally, I would create new minted environments for each particular font size you want to use. See the updated MWE:

\documentclass[12pt]{article}
\usepackage{minted}
\newminted[smalltext]{Text}{fontsize=\footnotesize}
\newminted[tinytext]{Text}{fontsize=\tiny}
\begin{document}

example 1

\begin{minted}{c} int main() { printf("hello, world"); return 0; } \end{minted}

example 2

\begin{smalltext} Methods for first order ODEs: --- Trying classification methods --- trying a quadrature &lt;- quadrature successful \end{smalltext}

example 3

\begin{tinytext} Methods for first order ODEs: --- Trying classification methods --- trying a quadrature &lt;- quadrature successful \end{tinytext}

\end{document}

Important lines are these:

\newminted[smalltext]{Text}{fontsize=\footnotesize}
\newminted[tinytext]{Text}{fontsize=\tiny}

They declare two environments, smalltext and tinytext. They use Text syntax and foot sizes that you used in the Minted environment originally.

You can then configure them in TeX4ht to produce some HTML code and style them using CSS:

\Preamble{xhtml,p-width}

%see https://tex.stackexchange.com/questions/554995/creating-better-html-code-with-minted-and-tex4ht

\ConfigureEnv{minted}{\NoFonts}{\EndNoFonts}{}{} \ConfigureEnv{smalltext}{\ifvmode\IgnorePar\fi\EndP\HCode{<div class="smalltext">}}{\ifvmode\IgnorePar\fi\EndP\HCode{</div>}}{}{} \Css{.smalltext{font-size:0.9rem;}} \ConfigureEnv{tinytext}{\ifvmode\IgnorePar\fi\EndP\HCode{<div class="tinytext">}}{\ifvmode\IgnorePar\fi\EndP\HCode{</div>}}{}{} \Css{.tinytext{font-size:0.7rem;}} \begin{document} \EndPreamble

This is the result:

enter image description here

michal.h21
  • 50,697