3

After doing some research on the interweb - I finally made the switch to minted. As I need to document Haskell with Latex, I created the following listing according to examples found on this page. (thx btw!)

\documentclass[a4paper]{report}
\usepackage[left=2.5cm,right=2.5cm]{geometry}

\usepackage{listings}
\usepackage{tcolorbox}
\usepackage{minted}

\tcbuselibrary{minted,skins}

\newtcblisting{haskellcode}[1][]{
  listing engine=minted,
  colback=bg,
  colframe=black!70,
  listing only,
  minted style=colorful,
  minted language=haskell,
  minted options={linenos=true,numbersep=3mm,texcl=true,#1},
  left=5mm,enhanced,
  overlay={\begin{tcbclipinterior}\fill[black!25] (frame.south west)
            rectangle ([xshift=5mm]frame.north west);\end{tcbclipinterior}}
}
\definecolor{bg}{rgb}{0.9,0.9,0.9}

\begin{document}
\begin{haskellcode}
{-# LANGUAGE OverloadedStrings     #-}
{-# LANGUAGE QuasiQuotes           #-}
{-# LANGUAGE TemplateHaskell       #-}
{-# LANGUAGE TypeFamilies          #-}
import Yesod

data HelloWorld = HelloWorld

mkYesod "HelloWorld" [parseRoutes|
/ HomeR GET
|]

instance Yesod HelloWorld

getHomeR :: Handler Html
getHomeR = defaultLayout [whamlet|Hello World!|]

main :: IO ()
main = warp 3000 HelloWorld
\end{haskellcode}
\end{document}

Unfortunately I strugle to get it to work.

\begin{haskellcode}
%{-# LANGUAGE OverloadedStrings     #-}
%{-# LANGUAGE QuasiQuotes           #-}
%{-# LANGUAGE TemplateHaskell       #-}
%{-# LANGUAGE TypeFamilies          #-}
import Yesod

data HelloWorld = HelloWorld
\end{haskellcode}

This example works like a charm, but as soon as I remove the comment %-operator, I get tonns of errors. In my opinion there is a problem, that pdftex is trying to parse the "{}"-brackets.

Many thanks for your help :).

Martin

3

This is due to two issues.

First, the way that \newtcblisting works, using it with optional arguments isn't possible. The new environment reads ahead looking for a [ that would indicate optional arguments, but reads past the end of the line and starts reading the actual code if it doesn't find a [. It is possible to capture an optional argument in such a situation, but that requires special techniques that tcolorbox isn't using. So if you want your environment to take any arguments, they will need to be mandatory (and you can always leave them empty, for example, \begin{haskellcode}{}).

Second, there is a bug in Pygments. The texcomments (texcl) option is causing everything after a %{-# at the beginning of a line to be treated as literal LaTeX code, up to the end of the line. This means that the closing #-} is not properly detected. You might report this at the Pygments site. For now, the solution is not to use texcomments with haskell.

2

The answer of G. Poole lists both problem: the optional only parameter for \newtcblisting and the Pygments bug.

For \newtcblisting there are two possibilites. G. Poole already explained the first one, i.e. use always at least one mandatory parameter. That is the way the tcolorbox package handles the tcblisting environment. In contrast to tcolorbox, this verbatim environment takes a mandatory parameter.

But there is a second possibility using xparse. For a optional only parameter, you can use the following:

\tcbuselibrary{minted,skins,xparse}

%\newtcblisting{haskellcode}[1][]{
\NewTCBListing{haskellcode}{O{}}{% <------ xparse
  listing engine=minted,
  colback=bg,
  colframe=black!70,
  listing only,
  minted style=colorful,
  minted language=haskell,
  minted options={linenos=true,numbersep=3mm,texcl=true,#1},
  left=5mm,enhanced,
  overlay={\begin{tcbclipinterior}\fill[black!25] (frame.south west)
            rectangle ([xshift=5mm]frame.north west);\end{tcbclipinterior}}
}

Of course, the Pygments problem still will be present, but now the haskellcode environment is more stable (thanks to xparse).

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.