1

My goal is to include lengthy example code that exceeds a single page, and I would like to set a background color for this code. The native verbatim environment allows content to span multiple pages, but it doesn't support background color, so I'm looking for alternative approaches.

I've explored some existing solutions on various forums, but none of them seem to meet my requirements:

  1. The method mentioned here involves using a box, which prevents the content from breaking across pages.
  2. The solution in this post does allow content to break across pages, but it introduces faint white lines between lines of code, affecting readability.
  3. This post also tried to solve the problem, but there is uncomfortable lines between snippet as well.(This post asked a similar question around 11 years ago but still no decent solution through its answers)
  4. Change the background color of native verbatim would not work either
  5. Other solutions, including utilizing mdframed to break a box across pages, still fail to fully address the issue.

Could you please advise me on how to proceed?


For example, implementing fancyvrb and box would be perfect with content less than 1 page, but how to make it better with longer code like below ?

\documentclass{article}
\usepackage{fancyvrb,newverbs,xcolor}

\definecolor{cverbbg}{gray}{0.93}

\newenvironment{lcverbatim}
 {\SaveVerbatim{cverb}}
 {\endSaveVerbatim
  \flushleft\fboxrule=0pt\fboxsep=.5em
  \colorbox{cverbbg}{%
    \makebox[\dimexpr\linewidth-2\fboxsep][l]{\BUseVerbatim{cverb}}%
  }
  \endflushleft
}

\begin{document}

\begin{lcverbatim}
// sensor_data.json
{
    "time": "2023-05-12T00:47:00.496Z", 
    "f1": "xxx1",
    "ws1": "WS1",
    "m1": "M1",
    "e1": "ETP",
    "o1": "OTP",
    "temp": 1.12"
}
// ingest spec part
{
  …,
  "dataSource": "sensor_data",
  "spec": {
    "dataSchema": {
      "timestampSpec": {
        "column": "time",
        "format": "iso"
      },
      "dimensionsSpec": {
        "dimensions": [
          "f1",
          "ws",
          "m1",
          "e1",
          "o1"
        ]
      },
      "metricsSpec": [
        {
          "name": "temp",
          "type": "doubleSum",
          "fieldName": "temp"
        }
      ]
    }
  }
}

// ingest spec part 2
{
  …,
  "dataSource": "sensor_data",
  "spec": {
    "dataSchema": {
      "timestampSpec": {
        "column": "time",
        "format": "iso"
      },
      "dimensionsSpec": {
        "dimensions": [
          "f1",
          "ws",
          "m1",
          "e1",
          "o1"
        ]
      },
      "metricsSpec": [
        {
          "name": "temp",
          "type": "doubleSum",
          "fieldName": "temp"
        }
      ]
    }
  }
}

\end{lcverbatim}

\end{document}
11
  • 1
    tcolorbox provides boxes with page breaks and also supports listings. BTW: It would be easier to give a concrete answer, if you would show, what you've tried, to see the problems you have had.
    – cabohah
    Commented Aug 8, 2023 at 7:36
  • @cabohah Thank you and I've added example code, really appreciate for your kindness
    – Xavier Z
    Commented Aug 8, 2023 at 8:07
  • @cabohah I found this post (tex.stackexchange.com/a/414034/302017) using tcolorbox as verbatim but faied to break page as well..
    – Xavier Z
    Commented Aug 8, 2023 at 8:18
  • 1
    tcolorbox should work fine, and instead of searching only the internet, look into the documentation. It has lots of examples. Commented Aug 8, 2023 at 8:33
  • 1
    A Q&A site is not a replacement for reading the documentation. Commented Aug 8, 2023 at 9:14

1 Answer 1

0

As already told: You can use tcolorbox, e.g.:

\documentclass{article}
\usepackage{tcolorbox}
\tcbuselibrary{breakable}
\tcbuselibrary{minted}% needs enabling of shell-escape

\begin{document}

\begin{tcblisting}{colback=gray!50,breakable,listing only}
// sensor_data.json
{
    "time": "2023-05-12T00:47:00.496Z", 
    "f1": "xxx1",
    "ws1": "WS1",
    "m1": "M1",
    "e1": "ETP",
    "o1": "OTP",
    "temp": 1.12"
}
// ingest spec part
{
  …,
  "dataSource": "sensor_data",
  "spec": {
    "dataSchema": {
      "timestampSpec": {
        "column": "time",
        "format": "iso"
      },
      "dimensionsSpec": {
        "dimensions": [
          "f1",
          "ws",
          "m1",
          "e1",
          "o1"
        ]
      },
      "metricsSpec": [
        {
          "name": "temp",
          "type": "doubleSum",
          "fieldName": "temp"
        }
      ]
    }
  }
}

// ingest spec part 2
{
  …,
  "dataSource": "sensor_data",
  "spec": {
    "dataSchema": {
      "timestampSpec": {
        "column": "time",
        "format": "iso"
      },
      "dimensionsSpec": {
        "dimensions": [
          "f1",
          "ws",
          "m1",
          "e1",
          "o1"
        ]
      },
      "metricsSpec": [
        {
          "name": "temp",
          "type": "doubleSum",
          "fieldName": "temp"
        }
      ]
    }
  }
}
\end{tcblisting}

\end{document}

enter image description here

or

\documentclass{article}
\usepackage{tcolorbox}
\tcbuselibrary{breakable}
\tcbuselibrary{listings}

\begin{document}

\begin{tcblisting}{colback=gray!50,breakable,listing only,listing options={basicstyle=\ttfamily,literate={…}{{\dots}}1}}
// sensor_data.json
{
    "time": "2023-05-12T00:47:00.496Z", 
    "f1": "xxx1",
    "ws1": "WS1",
    "m1": "M1",
    "e1": "ETP",
    "o1": "OTP",
    "temp": 1.12"
}
// ingest spec part
{
  …,
  "dataSource": "sensor_data",
  "spec": {
    "dataSchema": {
      "timestampSpec": {
        "column": "time",
        "format": "iso"
      },
      "dimensionsSpec": {
        "dimensions": [
          "f1",
          "ws",
          "m1",
          "e1",
          "o1"
        ]
      },
      "metricsSpec": [
        {
          "name": "temp",
          "type": "doubleSum",
          "fieldName": "temp"
        }
      ]
    }
  }
}

// ingest spec part 2
{
  …,
  "dataSource": "sensor_data",
  "spec": {
    "dataSchema": {
      "timestampSpec": {
        "column": "time",
        "format": "iso"
      },
      "dimensionsSpec": {
        "dimensions": [
          "f1",
          "ws",
          "m1",
          "e1",
          "o1"
        ]
      },
      "metricsSpec": [
        {
          "name": "temp",
          "type": "doubleSum",
          "fieldName": "temp"
        }
      ]
    }
  }
}
\end{tcblisting}

\end{document}

enter image description here

See the manuals of tcolorbox and listings for more information.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .