3

It seems like a lost case using Matlab code in Latex when it comes to tex4ht.

I switched to using matlab-prettifier package instead of mcode package to load Matlab code in Latex since I found too many problem with tex4ht using mcode. I already posted 2 questions on that. mcode-package-with-tex4ht-and-lualatex-produce-invalid-html-for-strings-in-matla and tex4ht-scrambles-some-matlab-code-when-using-mcode-package

Now I found another problem with using matlab-prettifier package. The problem is that string quotes are removed from Matlab code when using \lstinputlisting vs. when using lstlisting for same code.

I have tried changing the escapechar but nothing worked. Here is MWE

\documentclass[12pt]{article}%
\usepackage[T1]{fontenc}
\usepackage[utf8]{luainputenc}
\usepackage{amsmath,mathtools}
\usepackage[numbered,framed]{matlab-prettifier}
\lstset{
  style              = Matlab-editor,
  basicstyle         = \mlttfamily,
  escapechar         = `,
  mlshowsectionrules = true,
}

\begin{document}

\lstinputlisting{foo_x.m}

\begin{lstlisting}[language=Matlab,style=Matlab-editor]
% ode45 "is based on an explicit Runge-Kutta formula...
% program"
%

function foo_x
clear; clf; clc;
x=10;
fprinf('this is %d\n',x);
end
\end{lstlisting}
\end{document}

Same code is in the file foo_x.m as shown above inside the latex file. Exactly same listing. To save you time making foo_x.m and copy the code to it to reproduce this, here is link to the m file

This is what make4ht produces in HTML:

Mathematica graphics

Notice the difference from the first listing and the second listing for same code. The first one do not have single quote around the strings.

Compiled using

 make4ht --lua -u foo.tex

Here is what lualatex produces. Same code in both cases.

Mathematica graphics

I removed \usepackage[utf8]{luainputenc} from the MWE but it made no difference. I can keep trying adding and removing packages and changing options, but this is getting tiring really. It seems using Matlab code in Listing with make4ht --lua is a problem. When I remove the --lua option it works:

make4ht -u foo.tex

Mathematica graphics

but I need to use --lua option with make4ht. So what is the solution?

TL 2015 on Linux.

2

I have found the problem. \lstset do not work the same way with tex4ht as it does with lualatex. When I changed the the order of options to \lstset the problem went away! I simply changed

\lstset{
  language           = Matlab,
  style              = Matlab-editor,
  basicstyle         = \mlttfamily,
  escapechar         = `,
  mlshowsectionrules = true  
}

to

\lstset{
  basicstyle         = \mlttfamily,
  language           = Matlab,
  style              = Matlab-editor,
  escapechar         = `,
  mlshowsectionrules = true  
}

and now the string quotes are there! Moving language= Matlab above basicstyle= \mlttfamily brings the problem back. So the solution is this

\documentclass[12pt]{article}%    
\usepackage[T1]{fontenc}
\usepackage[utf8]{luainputenc}
\usepackage{amsmath,mathtools}           
\ifdefined\HCode
\usepackage{matlab-prettifier}
\lstset{
  basicstyle         = \mlttfamily,
  language           = Matlab,
  style              = Matlab-editor,
  escapechar         = `,
  mlshowsectionrules = true  
}
\else
\usepackage[numbered,framed]{matlab-prettifier}
\lstset{
  language           = Matlab,
  style              = Matlab-editor,
  basicstyle         = \mlttfamily,
  escapechar         = `,
  mlshowsectionrules = true  
}
\fi

\begin{document}

Now showing lstinputlisting

\lstinputlisting{foo_x.m}

Now showing lstlisting

\begin{lstlisting}[language=Matlab,style=Matlab-editor]
% ode45 "is based on an explicit Runge-Kutta formula...
% program"
%

function foo_x
clear; clf; clc;
x=10;
fprinf('this is %d\n',x);
end
\end{lstlisting}
\end{document}

Here is the result of make4ht --lua -u foo.tex

Mathematica graphics

Another problem I found, is that if I use \usepackage{times} then tex4ht also now loses the quotes around the code! Just adding \usepackage{times} in the MWE above causes the quotes to go away again.

So two things to watch for: do not use \usepackage{times} and make sure to put language = Matlab, after basicstyle= \mlttfamily when compiling with make4ht and --lua. I suspect these 2 things lead to the same basic encoding problem with tex4ht when using --lua option.

So many package clashes and option conflicts. It is a nightmare to keep track of all these things.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.