编辑

编辑

我正在做一些你可能认为有点奇怪但有充分理由的事情。下面的代码是由我编写的 lua 函数生成的。

由于某种原因,当我这样做超过 4 次时,它就会失败。

相同的代码在生成 4 次代码时编译成功。第 5 次时,我收到错误。这是 MWE(为了使 MWE 更小,我曾经将\def代码保存到其中,这似乎有效,而不是再次复制所有内容)

\documentclass[12pt]{scrbook}% 
\IfFileExists{luatex85.sty}
{
\usepackage{luatex85}
}
{}
\usepackage{longtable}
\usepackage{listings}
\usepackage{color}
\definecolor{bg}{RGB}{240,240,240}    
\usepackage{upquote} %to fix string quotes
\usepackage[most]{tcolorbox}
\tcbuselibrary{listings}
\usepackage{matlab-prettifier}

\begin{document} 

%this code below is autogenerated
\def\tbl{
\begin{longtable}{p{0.7\textwidth}}
{\newboxarray{mylisting}
\tcbinputlisting{nobeforeafter,enhanced,listing only,hbox,
listing file={toLua.txt},
size=small,store to box array=mylisting,listing options={framerule=0.05pt,numbers=left,
numberstyle=\scriptsize,basicstyle=\ttfamily\normalsize,
breaklines = false,showspaces=false}}
\boxarraygetwidth[mylisting]{\mylistingwidth}{1}
\tcbinputlisting{nobeforeafter,enhanced,listing only,
width=\mylistingwidth,left skip=5pt,
breakable,listing file={toLua.txt},size=small,
listing options={framerule=0.01pt,style=Matlab-editor,numbers=left,
numberstyle=\scriptsize,basicstyle=\ttfamily\normalsize
,breaklines = false,escapechar=`,showspaces=false,mlshowsectionrules = true}}
}
\end{longtable}
}

%make 5 copies, it fail. 4 copies is ok.
\tbl     
\tbl     
\tbl     
\tbl     
%\tbl   % uncomment to see the error !
\end{document}

4 次,编译成功并生成此 pdf

Mathematica 图形

当我取消注释第五份副本时,它无法编译,并出现错误

Package tcolorbox Warning: box array 'mylisting' is allocated again by \newboxa
rray. Memory and registers are wasted on input line 40.

(./toLua.txt) (./toLua.txt
Underfull \hbox (badness 10000) in paragraph at lines 3--4
[][][][][][][][][][][][][][][][][][][][][][][][][][][]
)

Package tcolorbox Warning: box array 'mylisting' is allocated again by \newboxa
rray. Memory and registers are wasted on input line 41.

(./toLua.txt) (./toLua.txt
Underfull \hbox (badness 10000) in paragraph at lines 3--4
[][][][][][][][][][][][][][][][][][][][][][][][][][][]
)
! Misplaced \noalign.
\pagebreak ->\noalign 
                      {\ifnum `}=0\fi \@testopt {\LT@no@pgbk -}4
l.41 \tbl
          % uncomment to see the error
? 

我看到了警告,box array 'mylisting' is allocated again by \newboxa rray. Memory and registers are wasted on但由于这是自动生成的代码,我在每个代码中使用相同的框数组mylisting(硬编码名称),因为我不知道如何在生成相同代码之前清除这个东西。但它不应该真的导致这种情况?但我不确定。也许mylisting每次清除框数组都会解决这个问题,但我不知道该怎么做。

上面使用的文件toLua.txt是这样的:(纯文本文件,包含小代码清单)

clear all;
s   = tf('s');
sys = ( s+1 )/(s^2  + s  +  1);
[num,den]=tfdata(sys,'v');
%convert to state space
[A,B,C,D] = tf2ss(num,den)

再次,我认为这是由于资源问题造成的,Package tcolorbox Warning:但我不知道如何解决这个警告。

上面的 tcolorbox 中使用的一些代码的参考,要感谢这里的答案使用 tcbinputlisting 在设置 hbox 时仅显示第一页

编辑

回应关于原始文档的评论。如果您确实需要表格,那么使用tabular而不是 就longtable可以了。但是,我个人不会仅仅因为您需要每个 中的多个代码块而引入这些复杂性\tbl。如果我不打算使用子图等,我只会使用minipages 或类似的。但毕竟这是你的文档,而不是我的!

答案1

您需要全局分配一次数组以避免出现警告。这不会修复由于此处不合适而导致的错误longtable。它正在尝试寻找分页符,但由于内容类型而无法找到。

最直接的解决方案可能是minipage。例如

\begin{filecontents*}{\jobname.txt}
clear all;
s   = tf('s');
sys = ( s+1 )/(s^2  + s  +  1);
[num,den]=tfdata(sys,'v');
%convert to state space
[A,B,C,D] = tf2ss(num,den)
\end{filecontents*}

\documentclass[12pt]{scrbook}%
\IfFileExists{luatex85.sty}
{
\usepackage{luatex85}
}
{}
\usepackage{longtable}
\usepackage{listings}
\usepackage{color}
\definecolor{bg}{RGB}{240,240,240}
\usepackage{upquote} %to fix string quotes
\usepackage[most]{tcolorbox}
\tcbuselibrary{listings}
\usepackage{matlab-prettifier}
\newboxarray{mylisting}
\begin{document}

%this code below is autogenerated
\newcommand*\tbl{%
  \begin{center}
    \begin{minipage}{0.7\textwidth}
      \tcbinputlisting{nobeforeafter,enhanced,listing only,hbox,
        listing file={\jobname.txt},
        size=small,store to box array=mylisting,listing options={framerule=0.05pt,numbers=left,
          numberstyle=\scriptsize,basicstyle=\ttfamily\normalsize,
          breaklines = false,showspaces=false}}%
      \boxarraygetwidth[mylisting]{\mylistingwidth}{1}%
      \tcbinputlisting{nobeforeafter,enhanced,listing only,
        width=\mylistingwidth,left skip=5pt,
        breakable,listing file={\jobname.txt},size=small,
        listing options={framerule=0.01pt,style=Matlab-editor,numbers=left,
          numberstyle=\scriptsize,basicstyle=\ttfamily\normalsize
          ,breaklines = false,escapechar=`,showspaces=false,mlshowsectionrules = true}}
    \end{minipage}
  \end{center}\medskip}

%make 5 copies, it fail. 4 copies is ok.
\tbl
\tbl
\tbl
\tbl
\tbl   % uncomment to see the error !
\end{document}

迷你页面

答案2

解决此问题的一种方法是\clearpage时不时地在您的 之间发出\tbl

建议

  • 在重复代码中,您通常不想创建新项目,例如\newcounter\newlength或如示例中所示\newboxarray。在命令之外和之前执行此操作。
  • 小心使用\def。它不会检查命令是否已存在。除非您知道自己在做什么,否则请使用\newcommand或。\NewDocumentCommand

完整代码

\documentclass[12pt]{scrbook}% 
\IfFileExists{luatex85.sty}
{
\usepackage{luatex85}
}
{}
\usepackage{longtable}
\usepackage{filecontents}
\usepackage{listings}
\usepackage{color}
\definecolor{bg}{RGB}{240,240,240}    
\usepackage{upquote} %to fix string quotes
\usepackage[most]{tcolorbox}
\tcbuselibrary{listings}
\usepackage{matlab-prettifier}
\begin{filecontents*}{toLua.txt}
clear all;
s   = tf('s');
sys = ( s+1 )/(s^2  + s  +  1);
[num,den]=tfdata(sys,'v');
%convert to state space
[A,B,C,D] = tf2ss(num,den)
}
\end{filecontents*}
\begin{document} 
\newboxarray{mylisting}
%this code below is autogenerated
\def\tbl{
\begin{longtable}{p{0.7\textwidth}}
{
\tcbinputlisting{nobeforeafter,enhanced,listing only,hbox,
listing file={toLua.txt},
size=small,store to box array=mylisting,listing options={framerule=0.05pt,numbers=left,
numberstyle=\scriptsize,basicstyle=\ttfamily\normalsize,
breaklines = false,showspaces=false}}
\boxarraygetwidth[mylisting]{\mylistingwidth}{1}
\tcbinputlisting{nobeforeafter,enhanced,listing only,
width=\mylistingwidth,left skip=5pt,
breakable,listing file={toLua.txt},size=small,
listing options={framerule=0.01pt,style=Matlab-editor,numbers=left,
numberstyle=\scriptsize,basicstyle=\ttfamily\normalsize
,breaklines = false,escapechar=`,showspaces=false,mlshowsectionrules = true}}
}
\end{longtable}
}

%make 5 copies, it fail. 4 copies is ok.
\tbl     
\tbl     
\tbl
\clearpage      
\tbl    
\tbl   % uncomment to see the error !
\end{document}

相关内容