我正在尝试将 .tex 文件包含到我的论文中。该文件是一些 MATLAB 代码,其格式已正确显示在 LaTeX 中。编译器会发出警告,指出代码对于页面来说太长,但不会自动将其拆分到多个页面中。我使用的代码是:
\begin{figure}
\input{SUB_GRID_FLAG_ML.tex}
\caption{SUB GRID FLAG ML, \hl{See APPENDIX FOR FULL CODE}}
\label{SUB_GRID_FLAG_ML_CODE}
\end{figure}
有没有办法在我的主 LaTeX 文件中而不是任何包含的 .tex 文件中执行此操作?
SUB_GRID_FLAG_ML.tex 的内容
% This file was automatically created from the m-file
% "m2tex.m" written by USL.
% The fontencoding in this file is UTF-8.
%
% You will need to include the following two packages in
% your LaTeX-Main-File.
%
% \usepackage{color}
% \usepackage{fancyvrb}
%
% It is advised to use the following option for Inputenc
% \usepackage[utf8]{inputenc}
%
% definition of matlab colors:
\definecolor{mblue}{rgb}{0,0,1}
\definecolor{mgreen}{rgb}{0.13333,0.5451,0.13333}
\definecolor{mred}{rgb}{0.62745,0.12549,0.94118}
\definecolor{mgrey}{rgb}{0.5,0.5,0.5}
\definecolor{mdarkgrey}{rgb}{0.25,0.25,0.25}
\DefineShortVerb[fontfamily=courier,fontseries=m]{\$}
\DefineShortVerb[fontfamily=courier,fontseries=b]{\#}
\noindent
\hspace*{-1.6em}{\scriptsize 1}$ $\color{mgreen}$%Set up a flag to alternate the nodes updated on different time steps.$\color{black}$$\\
\hspace*{-1.6em}{\scriptsize 2}$ subGridFlag = 0;$\\
\hspace*{-1.6em}{\scriptsize 3}$ $\\
\hspace*{-1.6em}{\scriptsize 4}$ $\color{mgreen}$%Set up a flag to alternate the nodes updated on different rows.$\color{black}$$\\
\hspace*{-1.6em}{\scriptsize 5}$ rowFlag = 0;$\\
\hspace*{-1.6em}{\scriptsize 6}$ $\\
\hspace*{-1.6em}{\scriptsize 7}$ $\color{mblue}$for$\color{black}$ t = 1:Ns$\\
\hspace*{-1.6em}{\scriptsize 8}$ $\color{mgreen}$%% RED NODES$\color{black}$$\\
\hspace*{-1.6em}{\scriptsize 9}$ if(subGridFlag == 0)$\\
\hspace*{-2em}{\scriptsize 10}$ $\color{mblue}$for$\color{black}$ l = 2:Nx-2$\\
\hspace*{-2em}{\scriptsize 11}$ $\color{mblue}$for$\color{black}$ m = 2:Ny-2$\\
\hspace*{-2em}{\scriptsize 12}$ if(rowFlag == 0)$\\
\hspace*{-2em}{\scriptsize 13}$ p(l,m) = (0.5*(p1(l-1,m)+p1(l+1,m)+p1(l,m-1)...$\\
\hspace*{-2em}{\scriptsize 14}$ +p1(l,m+1))-p2(l,m));$\\
\hspace*{-2em}{\scriptsize 15}$ rowFlag = 1;$\\
\hspace*{-2em}{\scriptsize 16}$ $\color{mblue}$else$\color{black}$$\\
\hspace*{-2em}{\scriptsize 17}$ p(l+1,m) = (0.5*(p1(l,m)+p1(l+2,m)+p1(l+1,m-1)...$\\
\hspace*{-2em}{\scriptsize 18}$ +p1(l+1,m+1))-p2(l+1,m));$\\
\hspace*{-2em}{\scriptsize 19}$ rowFlag = 0;$\\
\hspace*{-2em}{\scriptsize 20}$ $\color{mblue}$end$\color{black}$$\\
\hspace*{-2em}{\scriptsize 21}$ $\color{mblue}$end$\color{black}$$\\
\hspace*{-2em}{\scriptsize 22}$ $\\
\hspace*{-2em}{\scriptsize 23}$ $\color{mblue}$end$\color{black}$$\\
\hspace*{-2em}{\scriptsize 24}$ subGridFlag = 1;$\\
\hspace*{-2em}{\scriptsize 25}$ rowFlag = 0;$\\
\hspace*{-2em}{\scriptsize 26}$ $\\
\hspace*{-2em}{\scriptsize 27}$ $\color{mgreen}$% Get Output Value$\color{black}$$\\
\hspace*{-2em}{\scriptsize 28}$ out(t) = p(Outx,Outy);$\\
\hspace*{-2em}{\scriptsize 29}$ $\color{mgreen}$% Update mesh history$\color{black}$$\\
\hspace*{-2em}{\scriptsize 30}$ p2 = p1;$\\
\hspace*{-2em}{\scriptsize 31}$ p1 = p;$\\
\hspace*{-2em}{\scriptsize 32}$ $\color{mgreen}$%% BLUE NODES$\color{black}$$\\
\hspace*{-2em}{\scriptsize 33}$ $\color{mblue}$else$\color{black}$$\\
\hspace*{-2em}{\scriptsize 34}$ $\color{mblue}$for$\color{black}$ l = 2:Nx-2$\\
\hspace*{-2em}{\scriptsize 35}$ $\color{mblue}$for$\color{black}$ m = 2:Ny-2$\\
\hspace*{-2em}{\scriptsize 36}$ if(rowFlag == 0)$\\
\hspace*{-2em}{\scriptsize 37}$ p(l,m+1) = (0.5*(p1(l-1,m+1)+p1(l+1,m+1)+p1(l,m)...$\\
\hspace*{-2em}{\scriptsize 38}$ +p1(l,m+2))-p2(l,m+1));$\\
\hspace*{-2em}{\scriptsize 39}$ rowFlag = 1;$\\
\hspace*{-2em}{\scriptsize 40}$ $\color{mblue}$else$\color{black}$$\\
\hspace*{-2em}{\scriptsize 41}$ p(l+1,m+1) = (0.5*(p1(l,m+1)+p1(l+2,m+1)+p1(l+1,m)...$\\
\hspace*{-2em}{\scriptsize 42}$ +p1(l+1,m+2))-p2(l+1,m+1));$\\
\hspace*{-2em}{\scriptsize 43}$ rowFlag = 0;$\\
\hspace*{-2em}{\scriptsize 44}$ $\color{mblue}$end$\color{black}$$\\
\hspace*{-2em}{\scriptsize 45}$ $\color{mblue}$end$\color{black}$$\\
\hspace*{-2em}{\scriptsize 46}$ $\color{mblue}$end$\color{black}$$\\
\hspace*{-2em}{\scriptsize 47}$ subGridFlag = 0;$\\
\hspace*{-2em}{\scriptsize 48}$ rowFlag = 0;$\\
\hspace*{-2em}{\scriptsize 49}$ $\color{mgreen}$% Get Output Value$\color{black}$$\\
\hspace*{-2em}{\scriptsize 50}$ out(t) = p(Outx,Outy);$\\
\hspace*{-2em}{\scriptsize 51}$ $\color{mgreen}$% Update mesh history$\color{black}$$\\
\hspace*{-2em}{\scriptsize 52}$ p2 = p1;$\\
\hspace*{-2em}{\scriptsize 53}$ p1 = p;$\\
\hspace*{-2em}{\scriptsize 54}$ $\color{mblue}$end$\color{black}$$\\
\hspace*{-2em}{\scriptsize 55}$ $\color{mblue}$end$\color{black}$$\\
\UndefineShortVerb{\$}
\UndefineShortVerb{\#}
更新:我只是把我的flushleft
代码放错了地方,现在又恢复了格式。不幸的是,LaTeX 现在无论如何都会拆分所有代码,即使是 10 行的小代码块在页面底部附近调用时也会被拆分,而不是在下一页的顶部,从而失去了浮动环境的大部分布局质量。还有其他我可以尝试的技术吗?
答案1
类似这样的情况会让 TeX 尽量避免在行间断线,但确实允许断线,并允许在图形之前或之后拉伸空间,以使其更容易不断线。我使用了 50 这个值,使用更大的值(最高可达 10000)会使断线变得更加困难。
SG.tex
是你的文件,但是使用
\catcode`\$\active
\def${\verb$}
而不是MakeShortVerb
因为我不知道这个包来自哪里(您没有提供 MWE)并且 SG2 是相同的但在第 9 行被切断,以检查它没有破坏小例子。
下面的代码在顶部有标题,如果您希望它们在底部,那么:
\newcommand\minput[3]{%
\begin{flushleft}%
\filbreak
\interlinepenalty50
\input{#3}%
\par
\nobreak
\captionof{figure}{#1\label{#2}}%
\filbreak
\end{flushleft}}
原始版本带标题:
\documentclass{article}
\usepackage{color}
\usepackage{capt-of}
\newcommand\minput[3]{%
\begin{flushleft}%
\filbreak
\captionof{figure}{#1\label{#2}}%
\par
\nobreak
\interlinepenalty50
\input{#3}%
\filbreak
\end{flushleft}}
\begin{document}
\minput{SUB GRID FLAG ML}{SUB_GRID_FLAG_ML_CODE}{SG.tex}
\minput{SUB GRID FLAG ML 2}{SUB_GRID_FLAG_ML_CODE2}{SG2.tex}
aa\\
aa\\
aa\\
aa\\
aa\\aa\\
aa\\aa\\
aa\\
aa\\
aa
\minput{SUB GRID FLAG ML 3}{SUB_GRID_FLAG_ML_CODE3}{SG2.tex}
\end{document}