2

I am using tcolorbox with listings for code formating, which works well in pdf, but this package does not work with tex4ht (it produces invalid output).

I am having hard time figuring the latex magic code needed to tell Latex what to do when compiling with tex4ht vs. pdf, without having to duplicate the code fragment itself.

A small example will help. Currently this is what I have

\documentclass[12pt]{book}%
 ...
\begin{document}
\newtcblisting{....}{....code is here ...}  

The problem is that \newtcblisting is an environment like from the tcolorbox package, the full definition of the above is below (MWE)

\documentclass[12pt]{book}%
\usepackage[T1]{fontenc}
\usepackage{color}
\usepackage{listings}
\usepackage{tcolorbox}
\tcbuselibrary{listings}
\usepackage{matlab-prettifier}
\definecolor{bg}{RGB}{240,240,240}
\usepackage{upquote} %to fix string quotes
\usepackage{fancyvrb}    

\newtcblisting{matlab}[1]{
   nobeforeafter,colback=bg,size=minimal,hbox,listing only,
   listing options={style=Matlab-editor,basicstyle=\ttfamily#1,
      breaklines= false,escapechar= `,mlshowsectionrules = true
   }}

\begin{document}
\begin{matlab}{\small}
clear all;
m_zeros = [-1 -2];
m_poles = [0 -4 -6];
\end{matlab}
\end{document}

And the problem is, how to define new environment, which will allow me to do something different in tex4ht, without having to duplicate the code itself. i.e. I can do this ofcourse:

 ...
 \ifdefined\HCode
  \begin{verbatim}%font size option not important, so no need to pass it
    ... code listing....
  \end{verbatim}
 \else
  \begin{matlab}{\small}%in pdf using tcblisting
      ... code listing....
  \end{matlab}
\fi
.....

But I do not want to duplicate the code itself, as the code fragments can be large and the document is already complicated as is. I also do not want to put these code fragments in files, and read them from files for number of reasons.

And this is where I am stuck. I'd like this do:

....
\ifdefined\HCode 
   \newenvironment{matlab}[1]
   {\begin{verbatim}}
   {\end{verbatim}}
\else
\newtcblisting{matlab}[1]{
....

But the above does not work, since verbatim environment is not allowed there. SO I tried this instead

\DefineVerbatimEnvironment{matlabX}{Verbatim}{fontsize=\small}    
\ifdefined\HCode 
   \newenvironment{matlab}[1]
   {\begin{matlabX}}
   {\end{matlabX}}
\else
\newtcblisting{matlab}[1]{
 .....

And this gives syntax error with tex4ht. I am way over head at this point and reached my limit in Latex code magic. Do not know what to do.

Question is: Is it possible to define new environment, say just verbatim for now, which kicks in when tex4ht is running, without having to duplicate the code itself?

The file is compiled to HTML using the command make4ht foo.tex and this generates foo.html in same folder. Compiled to pdf using lualatex foo.tex.

  • Do you really want fontenc if you're using LuaLaTeX? Seems rather odd. You might need it - if you really know what you're doing. Otherwise, probably not. – cfr Jun 10 '16 at 3:11
5

Why can't you just use a new listings environment since you are loading it anyway, for the tex4ht case?

\documentclass[12pt]{book}%
\usepackage[T1]{fontenc}
\usepackage{color}
\usepackage{listings}
\usepackage{matlab-prettifier}
\definecolor{bg}{RGB}{240,240,240}
\usepackage{upquote} %to fix string quotes
\usepackage{fancyvrb}
\ifdefined\HCode
\lstnewenvironment{matlab}
{}
{}
\else
\usepackage{tcolorbox}
\tcbuselibrary{listings}
\newtcblisting{matlab}[1]{%
  nobeforeafter,
  colback=bg,
  size=minimal,
  hbox,
  listing only,
  listing options={%
    style=Matlab-editor,
    basicstyle=\ttfamily#1,
    breaklines= false,
    escapechar= `,
    mlshowsectionrules = true,
  },
}
\fi
\begin{document}
\begin{matlab}{\small}
clear all;
m_zeros = [-1 -2];
m_poles = [0 -4 -6];
\end{matlab}
\end{document}
  • Tested using htlatex for HTML and pdflatex for PDF. – cfr Jun 10 '16 at 3:10
3

Here is a way using moreverb package:

\documentclass[12pt]{book}%
\usepackage[T1]{fontenc}
\usepackage{color}
\usepackage{listings}
\usepackage{tcolorbox}
\tcbuselibrary{listings}
\usepackage{matlab-prettifier}
\definecolor{bg}{RGB}{240,240,240}
\usepackage{upquote} %to fix string quotes

\newtcblisting{matlab}[1]{
   nobeforeafter,colback=bg,listing only,
   listing options={style=Matlab-editor,basicstyle=\ttfamily#1,
      breaklines= false,escapechar= `,mlshowsectionrules = true
   }}

\def\putVerb{%
  \HCode{<PRE>}%
  \verbatiminput{a.tex}%
  \HCode{</PRE>}%
}

\ifdefined\HCode
\usepackage{moreverb}
  \newenvironment{code}
  {\verbatimwrite{a.tex}}
  {\endverbatimwrite
  \aftergroup\putVerb}
\else
\usepackage{fancyvrb}
  \newenvironment{code}
  {\matlab{\small}}
  {\endmatlab}
\fi

\begin{document}
\begin{code}
clear all;
m_zeros = [-1 -2];
m_poles = [0 -4 -6];
\end{code}
and
\begin{code}
clear all;
m_poles = [0 -4 -6];
m_zeros = [-1 -2];
\end{code}

\end{document}

Tested both htlatex and pdflatex.

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.