0

I have the following elm file with some source code, that I would like to include in listings:

-- >Color
type Color = Red | Green | Blue
-- <Color

-- >colorToString
colorToStirng : Color -> String
colorToString color
  case color of
    Red -> "Red"
    Green -> "Green"
    Blue -> "Blue"
-- <colorToString

-- >colorToHex
colorToHex : Color -> (Int, Int, Int)
colorToHex color =
  case color of
    Red -> (255, 0, 0)
    Green -> (0, 255, 0)
    Blue -> (0, 0, 255)
-- <colorToHex

With the configuration file, where I have the syntax definition for Elm and an environment that is set up so I can just type the function or type name I want to include:

% config.sty
\lstdefinelanguage{elm}{
  morekeywords=[1]{type, module, exposing, import, alias, case, of, let, in, if, else, then},
  morekeywords=[2]{Html, Msg, update, view, main, model, onClick, div, button, text},
  morekeywords=[3]{String, Bool, Int, Float, number, number1, number2, number3, comparable, Order, appendable, List},
  sensitive,
  morecomment=[s]{\{-}{-\}},
  morecomment=[l]--,
  morestring=[b]',
  morestring=[b]"
}

\newtcbinputlisting{\inputbox}[3]{%
    listing file={#3},
    title=Listing (\thetcbcounter) of \texttt{#3},
    colframe=orange!70!black,
    listing only,
    after title={\hfill\small\texttt{#2 code}},
    listing options={
        linerange=\-\-\ >#1-\-\-\ <#1,
        includerangemarker=false,
        language=#2
}

and the main source file:

% doc.tex
\documentclass[12pt,a4paper,twoside]{report}
\usepackage[margin=2cm]{geometry}
\usepackage[utf8]{inputenc}
\usepackage{hyperref}
\usepackage{float}
\usepackage{listings}
\usepackage{titlesec}
\usepackage{tcolorbox}
\tcbuselibrary{listings}
\usepackage{parskip}
\usepackage{booktabs}
\usepackage{etoolbox}
\usepackage{config}

\begin{document}

\tableofcontents

\inputbox{Color}{Elm}{Types.elm}

\end{document}

This works ok if I only want a single function or type, but I would like to have 2 listings, where each should contain the type definition for Color and one of the functions, something like this:

\inputbox{Color, colorToString}{Elm}{Types.elm}
\inputbox{Color, colorToHex}{Elm}{Types.elm}

without changing the environment to:

% config.sty
\newtcbinputlisting{\inputbox}[3]{%
    listing file={#3},
    title=Listing (\thetcbcounter) of \texttt{#3},
    colframe=orange!70!black,
    listing only,
    after title={\hfill\small\texttt{#2 code}},
    listing options={
        linerange=#1,
        includerangemarker=false,
        language=#2
}

and writing:

\inputbox{\-\-\ >Color-\-\-\ <Color,\-\-\ >colorToString-\-\-\ <colorToString}{Elm}{Types.elm}
\inputbox{\-\-\ >Color-\-\-\ <Color,\-\-\ >colorToHex-\-\-\ <colorToHex}{Elm}{Types.elm}

I have tried to define a macro to generate the argument to linerange:

% config.sty
\newcommand{\range}[1]{\-\-\ >#1-\-\-\ <#1}

with the environment updated:

% config.sty
\newtcbinputlisting{\inputbox}[3]{%
    listing file={#3},
    title=Listing (\thetcbcounter) of \texttt{#3},
    colframe=orange!70!black,
    listing only,
    after title={\hfill\small\texttt{#2 code}},
    listing options={
        linerange=#1,
        includerangemarker=false,
        language=#2
}

to use it like this (which seems like a decent compromise):

\inputbox{\range{Color},\range{colorToString}}{Elm}{Types.elm}
\inputbox{\range{Color},\range{colorToHex}}{Elm}{Types.elm}

but I keep getting errors. For a full example with both versions, see this overleaf project https://www.overleaf.com/project/5f6afbf4d47e2e000102c048

  • Welcome to TeX.SE! – Mensch Sep 23 at 8:49

Your Answer

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

Browse other questions tagged or ask your own question.