左侧框架 tcolotbox 在被新页面拆分时消失

左侧框架 tcolotbox 在被新页面拆分时消失

在下面给出的代码中,如果 tcolorbox 有分页符,我想要的左侧灰线就会消失。但是,如果 tcolorbox 没有分页符,则左侧线就会出现。

在此处输入图片描述

当 tcolorbox 损坏时,您能帮我让左边的灰线也显示出来吗?谢谢!

代码

\documentclass[a4paper, 10pt, oneside, fleqn, openright]{book}
\usepackage[no-math]{fontspec}

\usepackage{polyglossia}
\setdefaultlanguage{french}
\setotherlanguages{english}

\newcommand{\codeimg}{$\vcenter{\hbox{\includegraphics[height=\baselineskip]{example-image-a}}}$}
\newcommand{\codeimgpy}{$\vcenter{\hbox{\includegraphics[height=\baselineskip]{example-image-b}}}$}
\newcommand{\codeimgcpp}{$\vcenter{\hbox{\includegraphics[height=\baselineskip]{example-image-c}}}$}

\usepackage{calc}
\usepackage[usenames,dvipsnames,svgnames,table]{xcolor}
\usepackage{pdfpages,graphicx}
\usepackage{mdframed}
\usepackage{listings}
\usepackage{filecontents}
\definecolor{mybluei}{RGB}{0,173,239}
\definecolor{myblueii}{RGB}{63,200,244}
\definecolor{myblueiii}{RGB}{199,234,253}
\definecolor{light-gray}{gray}{0.92}

\definecolor{mainColor}{RGB}{211, 47, 47} % some dark red

\renewcommand\lstlistingname{Code}
\lstset{
    language=Python,
    numbers=left,
    numbersep= 7mm,
    numberstyle=\color{Black},
    stepnumber=1,
    tabsize=3,
    breakatwhitespace=false,
    breaklines=true,
    captionpos=b,
    basicstyle=\color{Black}\ttfamily,
    commentstyle=\color{LimeGreen},
    keywordstyle=\color{BurntOrange}\bfseries,
    stringstyle=\color{WildStrawberry},
    keywords={var, func, extends},
    frame=leftline,
    framesep=0mm,
    xleftmargin=3mm,
    framesep=2mm, 
    framerule=0mm,
    abovecaptionskip=5mm,
    aboveskip=\baselineskip,
    belowskip=\baselineskip
}

\usepackage{tcolorbox}
\newcounter{data}
\newcounter{result}
\newcounter{pythoncode}
\newcounter{cppcode}
\newcounter{matlab}
\tcbuselibrary{skins,breakable,listings}

\newtcbinputlisting[use counter=matlab,list inside=matlab,number within=chapter]{\inputmatlab}[3][]{listing options={style=Matlab-editor},%
        enhanced,noparskip,breakable,colback=light-gray,colframe=DarkSlateGray,opacitybacktitle=.8,%
        fonttitle=\bfseries,before upper={\hspace*{-1em}\codeimg~#2},%
    title after break={\centering\footnotesize\itshape\strut Matlab Code~\thematlab~--~continued},%
     listing only,listing options={xleftmargin=-1mm,#1,style=Matlab-editor},
     after upper={\centering\strut {\bfseries Matlab Code~\thematlab:}~#2},
     frame hidden,arc=0pt,outer arc=0pt,boxrule=0pt,frame code={\draw[gray,line width=2mm] ([xshift=-0.5pt]frame.north west) -- ([xshift=-0.5pt]frame.south west);},
     %
     listing file={#3},#1}

\newtcbinputlisting[use counter=cppcode,list inside=cppcode,number within=chapter]{\inputcppcode}[3][]{listing options={language=C++},%
        enhanced,noparskip,breakable,colback=light-gray,colframe=DarkSlateGray,opacitybacktitle=.8,%
        fonttitle=\bfseries,before upper={\hspace*{-1em}\codeimgcpp~#2},%
    title after break={\centering\footnotesize\itshape\strut C++ Code~\thecppcode~--~continued},%
     listing only,listing options={xleftmargin=-1mm,#1,language=C++},
     after upper={\centering\strut {\bfseries C++ Code~\thecppcode:}~#2},
     frame hidden,arc=0pt,outer arc=0pt,boxrule=0pt,frame code={\draw[gray,line width=2mm] ([xshift=-0.5pt]frame.north west) -- ([xshift=-0.5pt]frame.south west);},
     %
     listing file={#3},#1}

\usepackage[numbered,framed]{matlab-prettifier}

\begin{filecontents*}{sample.m}
% create a file for output
!touch testFile.txt
fid = fopen('testFile.text', 'w')
for i=1:10
  fprintf(fid,'%6.2f \n', i);
end
\end{filecontents*}

\begin{filecontents*}{cppcodex.cpp}
#include <iostream>
using namespace std;

int main()
{
    int firstNumber, secondNumber, sumOfTwoNumbers;

    cout << "Enter two integers: ";
    cin >> firstNumber >> secondNumber;

    // sum of two numbers in stored in variable sumOfTwoNumbers
    sumOfTwoNumbers = firstNumber + secondNumber;

    // Prints sum
    cout << firstNumber << " + " <<  secondNumber << " = " << sumOfTwoNumbers;

    return 0;
}
\end{filecontents*}

\begin{document}

\inputmatlab{Expansion Algorithm}{sample.m}

\vspace*{7cm}

\inputcppcode{Example C++ Code}{cppcodex.cpp}

\end{document}

答案1

frame hidden另一种解决方法。boxrule=0pt不要隐藏框架,而是定义一个leftrule=...在破损的框中幸存下来的特定的框。

\documentclass[a4paper, 10pt, oneside, fleqn, openright]{book}
\usepackage[no-math]{fontspec}

\usepackage{polyglossia}
\setdefaultlanguage{french}
\setotherlanguages{english}

\newcommand{\codeimg}{$\vcenter{\hbox{\includegraphics[height=\baselineskip]{example-image-a}}}$}
\newcommand{\codeimgpy}{$\vcenter{\hbox{\includegraphics[height=\baselineskip]{example-image-b}}}$}
\newcommand{\codeimgcpp}{$\vcenter{\hbox{\includegraphics[height=\baselineskip]{example-image-c}}}$}

\usepackage{calc}
\usepackage[usenames,dvipsnames,svgnames,table]{xcolor}
\usepackage{pdfpages,graphicx}
\usepackage{mdframed}
\usepackage{listings}
\usepackage{filecontents}
\definecolor{mybluei}{RGB}{0,173,239}
\definecolor{myblueii}{RGB}{63,200,244}
\definecolor{myblueiii}{RGB}{199,234,253}
\definecolor{light-gray}{gray}{0.92}

\definecolor{mainColor}{RGB}{211, 47, 47} % some dark red

\renewcommand\lstlistingname{Code}
\lstset{
    language=Python,
    numbers=left,
    numbersep= 7mm,
    numberstyle=\color{Black},
    stepnumber=1,
    tabsize=3,
    breakatwhitespace=false,
    breaklines=true,
    captionpos=b,
    basicstyle=\color{Black}\ttfamily,
    commentstyle=\color{LimeGreen},
    keywordstyle=\color{BurntOrange}\bfseries,
    stringstyle=\color{WildStrawberry},
    keywords={var, func, extends},
    frame=leftline,
    framesep=0mm,
    xleftmargin=3mm,
    framesep=2mm, 
    framerule=0mm,
    abovecaptionskip=5mm,
    aboveskip=\baselineskip,
    belowskip=\baselineskip
}

\usepackage{tcolorbox}
\newcounter{data}
\newcounter{result}
\newcounter{pythoncode}
\newcounter{cppcode}
\newcounter{matlab}
\tcbuselibrary{skins,breakable,listings}

\newtcbinputlisting[use counter=matlab,list inside=matlab,number within=chapter]{\inputmatlab}[3][]{listing options={style=Matlab-editor},%
        enhanced,noparskip,breakable,colback=light-gray,colframe=DarkSlateGray,opacitybacktitle=.8,%
        fonttitle=\bfseries,before upper={\hspace*{-1em}\codeimg~#2},%
    title after break={\centering\footnotesize\itshape\strut Matlab Code~\thematlab~--~continued},%
     listing only,listing options={xleftmargin=-1mm,#1,style=Matlab-editor},
     after upper={\centering\strut {\bfseries Matlab Code~\thematlab:}~#2},
     %frame hidden,   %<---------------------------------------
     arc=0pt,
     outer arc=0pt,
     boxrule=0pt,
     leftrule=1mm,  %<----------------------------
%     frame code={\draw[gray,line width=2mm] ([xshift=-0.5pt]frame.north west) -- ([xshift=-0.5pt]frame.south west);},
     %
     listing file={#3},#1}

\newtcbinputlisting[use counter=cppcode,list inside=cppcode,number within=chapter]{\inputcppcode}[3][]{listing options={language=C++},%
        enhanced,noparskip,breakable,colback=light-gray,colframe=DarkSlateGray,opacitybacktitle=.8,%
        fonttitle=\bfseries,before upper={\hspace*{-1em}\codeimgcpp~#2},%
    title after break={\centering\footnotesize\itshape\strut C++ Code~\thecppcode~--~continued},%
     listing only,listing options={xleftmargin=-1mm,#1,language=C++},
     after upper={\centering\strut {\bfseries C++ Code~\thecppcode:}~#2},
%     frame hidden, %<------------------------------
     arc=0pt,
     outer arc=0pt,
     boxrule=0pt,
     leftrule=1mm, %<---------------------------------
     %frame code={\draw[gray,line width=2mm] ([xshift=-0.5pt]frame.north west) -- ([xshift=-0.5pt]frame.south west);},
     %
     listing file={#3},#1}

\usepackage[numbered,framed]{matlab-prettifier}

\begin{filecontents*}{sample.m}
% create a file for output
!touch testFile.txt
fid = fopen('testFile.text', 'w')
for i=1:10
  fprintf(fid,'%6.2f \n', i);
end
\end{filecontents*}

\begin{filecontents*}{cppcodex.cpp}
#include <iostream>
using namespace std;

int main()
{
    int firstNumber, secondNumber, sumOfTwoNumbers;

    cout << "Enter two integers: ";
    cin >> firstNumber >> secondNumber;

    // sum of two numbers in stored in variable sumOfTwoNumbers
    sumOfTwoNumbers = firstNumber + secondNumber;

    // Prints sum
    cout << firstNumber << " + " <<  secondNumber << " = " << sumOfTwoNumbers;

    return 0;
}
\end{filecontents*}

\begin{document}

\inputmatlab{Expansion Algorithm}{sample.m}

\vspace*{7cm}

\inputcppcode{Example C++ Code}{cppcodex.cpp}

\end{document}

在此处输入图片描述

答案2

我不知道为什么你的 tex 代码不能按预期工作。

exras={frame code={\draw[gray,line width=2mm] ([xshift=-0.5pt]frame.north west) -- ([xshift=-0.5pt]frame.south west);}}我建议使用within 的解决方法彩色盒子选项:

\documentclass[a4paper, 10pt, oneside, fleqn, openright]{book}
\usepackage[no-math]{fontspec}
\usepackage{lipsum}
\usepackage{polyglossia}
\setdefaultlanguage{french}
\setotherlanguages{english}

\newcommand{\codeimg}{$\vcenter{\hbox{\includegraphics[height=\baselineskip]{example-image-a}}}$}
\newcommand{\codeimgpy}{$\vcenter{\hbox{\includegraphics[height=\baselineskip]{example-image-b}}}$}
\newcommand{\codeimgcpp}{$\vcenter{\hbox{\includegraphics[height=\baselineskip]{example-image-c}}}$}

\usepackage{calc}
\usepackage[usenames,dvipsnames,svgnames,table]{xcolor}
\usepackage{pdfpages,graphicx}
\usepackage{mdframed}
\usepackage{listings}
\usepackage{filecontents}
\definecolor{mybluei}{RGB}{0,173,239}
\definecolor{myblueii}{RGB}{63,200,244}
\definecolor{myblueiii}{RGB}{199,234,253}
\definecolor{light-gray}{gray}{0.92}

\definecolor{mainColor}{RGB}{211, 47, 47} % some dark red

\renewcommand\lstlistingname{Code}
\lstset{
    language=Python,
    numbers=left,
    numbersep= 7mm,
    numberstyle=\color{Black},
    stepnumber=1,
    tabsize=3,
    breakatwhitespace=false,
    breaklines=true,
    captionpos=b,
    basicstyle=\color{Black}\ttfamily,
    commentstyle=\color{LimeGreen},
    keywordstyle=\color{BurntOrange}\bfseries,
    stringstyle=\color{WildStrawberry},
    keywords={var, func, extends},
    frame=leftline,
    framesep=0mm,
    xleftmargin=3mm,
    framesep=2mm, 
    framerule=0mm,
    abovecaptionskip=5mm,
    aboveskip=\baselineskip,
    belowskip=\baselineskip
}

\usepackage{tcolorbox}
\newcounter{data}
\newcounter{result}
\newcounter{pythoncode}
\newcounter{cppcode}
\newcounter{matlab}
\tcbuselibrary{skins,breakable,listings}

\newtcbinputlisting[use counter=matlab,list inside=matlab,number within=chapter]{\inputmatlab}[3][]{listing options={style=Matlab-editor},%
        enhanced,noparskip,breakable,colback=light-gray,colframe=DarkSlateGray,opacitybacktitle=.8,%
        fonttitle=\bfseries,before upper={\hspace*{-1em}\codeimg~#2},%
    title after break={\centering\footnotesize\itshape\strut Matlab Code~\thematlab~--~continued},%
     listing only,listing options={xleftmargin=-1mm,#1,style=Matlab-editor},
     after upper={\centering\strut {\bfseries Matlab Code~\thematlab:}~#2},
     frame hidden,arc=0pt,outer arc=0pt,boxrule=0pt,
     extras={frame code={\draw[gray,line width=2mm] ([xshift=-0.5pt]frame.north west) -- ([xshift=-0.5pt]frame.south west);}},
     %
     listing file={#3},#1}

\newtcbinputlisting[use counter=cppcode,list inside=cppcode,number within=chapter]{\inputcppcode}[3][]{listing options={language=C++},%
        enhanced,noparskip,breakable,colback=light-gray,colframe=DarkSlateGray,opacitybacktitle=.8,%
        fonttitle=\bfseries,before upper={\hspace*{-1em}\codeimgcpp~#2},%
    title after break={\centering\footnotesize\itshape\strut C++ Code~\thecppcode~--~continued},%
     listing only,listing options={xleftmargin=-1mm,#1,language=C++},
     after upper={\centering\strut {\bfseries C++ Code~\thecppcode:}~#2},
     frame hidden,arc=0pt,outer arc=0pt,boxrule=0pt,
     extras={frame code={\draw[gray,line width=2mm] ([xshift=-0.5pt]frame.north west) -- ([xshift=-0.5pt]frame.south west);}},
     %
     listing file={#3},#1}

\usepackage[numbered,framed]{matlab-prettifier}

\begin{filecontents*}{sample.m}
% create a file for output
!touch testFile.txt
fid = fopen('testFile.text', 'w')
for i=1:10
  fprintf(fid,'%6.2f \n', i);
end

\end{filecontents*}

\begin{filecontents*}{cppcodex.cpp}
#include <iostream>
using namespace std;

int main()
{
    int firstNumber, secondNumber, sumOfTwoNumbers;

    cout << "Enter two integers: ";
    cin >> firstNumber >> secondNumber;

    // sum of two numbers in stored in variable sumOfTwoNumbers
    sumOfTwoNumbers = firstNumber + secondNumber;

    // Prints sum
    cout << firstNumber << " + " <<  secondNumber << " = " << sumOfTwoNumbers;

    return 0;
}
\end{filecontents*}

\begin{document}

\inputmatlab{Expansion Algorithm}{sample.m}

\vspace*{7cm}

\inputcppcode{Example C++ Code}{cppcodex.cpp}

\end{document}

在此处输入图片描述

相关内容