如何为长度超过单页的逐字格式化内容设置背景颜色?

如何为长度超过单页的逐字格式化内容设置背景颜色?

我的目标是包含超过一页的长示例代码,并且我想为这段代码设置背景颜色。本机逐字环境允许内容跨越多个页面,但不支持背景颜色,因此我正在寻找替代方法。

我已经在各个论坛上探索了一些现有的解决方案,但似乎都没有满足我的要求:

  1. 提到的方法这里涉及使用一个框,它可以防止内容跨页面。
  2. 解决方案这个帖子确实允许内容跨页,但它会在代码行之间引入模糊的白线,影响可读性。
  3. 邮政也尝试过解决这个问题,但是片段之间也存在不舒服的线条。(这个帖子大约 11 年前问过类似的问题,但仍然没有通过其答案找到合适的解决方案)
  4. 更改原生逐字的背景颜色也不起作用
  5. 其他解决方案,包括利用 mdframed 来跨页面拆分框,仍然无法完全解决该问题。

您能告诉我下一步该怎么做吗?


例如,如果内容少于 1 页,实现 fancyvrb 和 box 会很完美,但是如何使用像下面这样的更长的代码使其变得更好?

\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}

答案1

正如已经说过的:您可以使用tcolorbox,例如:

\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}

在此处输入图片描述

或者

\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}

在此处输入图片描述

tcolorbox请参阅和的手册listings以了解更多信息。

相关内容