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:
- The method mentioned here involves using a box, which prevents the content from breaking across pages.
- The solution in this post does allow content to break across pages, but it introduces faint white lines between lines of code, affecting readability.
- 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)
- Change the background color of native verbatim would not work either
- 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}
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.