使用 tcolorbox 与 c++ #if #endif 指令时出现问题

使用 tcolorbox 与 c++ #if #endif 指令时出现问题

我用它tcolorbox来突出显示c++文档中的代码。有人能解释一下为什么编译器会抱怨该#if指令吗?我得到的错误是:

Extra }, or forgotten $ \PY{c+cp}{\PYZsh{}if VTK_MAJOR_VERSION <= 5}

当我注释指令时,一切都正常。以下是 MWE。

\documentclass[11pt,letterpaper]{article}

\usepackage[left=1.00in, right=1.00in, top=1.00in, bottom=1.00in]{geometry}

\usepackage{tcolorbox}
\tcbuselibrary{minted,skins,breakable}

\newtcblisting{cppcode}[1][]{
  listing engine=minted,
  breakable,
  colback=bg,
  colframe=black!70,
  listing only,
  minted style=colorful,
  minted language=c++,
  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.85,0.85,0.85}

\begin{document}

\begin{cppcode}[]
#if VTK_MAJOR_VERSION <= 5
    streamLine->SetInput( sgrid );
    streamLine->SetSource( seeds->GetOutput() );
#else   
    streamLine->SetInputData( sgrid );
    streamLine->SetSourceConnection( seeds->GetOutputPort() );
#endif
\end{cppcode}

\end{document}

答案1

问题是您设置texcltrue,这会导致问题(在第一行中,#下划线的字符VTK_MAJOR_VERSION被解释为数学模式下标字符,并且由于它们出现在文本模式下,因此会触发错误)。

您有几种可能的解决方案:

  1. 全局删除该选项或在本地禁用它:

    \documentclass[11pt,letterpaper]{article}
    
    \usepackage[left=1.00in, right=1.00in, top=1.00in, bottom=1.00in]{geometry}
    
    \usepackage{tcolorbox}
    \tcbuselibrary{minted,skins,breakable}
    
    \newtcblisting{cppcode}[1][]{
      listing engine=minted,
      breakable,
      colback=bg,
      colframe=black!70,
      listing only,
      minted style=colorful,
      minted language=c++,
      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.85,0.85,0.85}
    
    \begin{document}
    
    \begin{cppcode}[texcl=false]
    #if VTK_MAJOR_VERSION <= 5
        streamLine->SetInput( sgrid );
        streamLine->SetSource( seeds->GetOutput() );
    #else   
        streamLine->SetInputData( sgrid );
        streamLine->SetSourceConnection( seeds->GetOutputPort() );
    #endif
    \end{cppcode}
    
    \end{document}
    

    在此处输入图片描述

  2. 转义下划线,使其不被解释为下标的数学模式字符:

    \documentclass[11pt,letterpaper]{article}
    
    \usepackage[left=1.00in, right=1.00in, top=1.00in, bottom=1.00in]{geometry}
    
    \usepackage{tcolorbox}
    \tcbuselibrary{minted,skins,breakable}
    
    \newtcblisting{cppcode}[1][]{
      listing engine=minted,
      breakable,
      colback=bg,
      colframe=black!70,
      listing only,
      minted style=colorful,
      minted language=c++,
      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.85,0.85,0.85}
    
    \begin{document}
    
    \begin{cppcode}[]
    #if VTK\_MAJOR\_VERSION <= 5
        streamLine->SetInput( sgrid );
        streamLine->SetSource( seeds->GetOutput() );
    #else   
        streamLine->SetInputData( sgrid );
        streamLine->SetSourceConnection( seeds->GetOutputPort() );
    #endif
    \end{cppcode}
    
    \end{document}
    

相关内容