如何使得algorithm环境写的算法可跨页

文摘   2025-01-05 20:17   浙江  

由于浮动体环境不能跨页,因此在排版算法时,过长的代码块会被分隔不同的页面上。Tabular可通过Longtable宏进行跨页排版,图片则可以缩放大小或者运用子图,那么,算法块是否有对应的解决方案呢?

选自:https://ask.latexstudio.net/ask/question/17588.html

【解决方案】

实现代码如下:

\documentclass{article}\usepackage{algorithm,algpseudocode,float}\usepackage{lipsum}\usepackage{amsmath}\usepackage{hyperref}\makeatletter\newenvironment{breakablealgorithm}  {% \begin{breakablealgorithm}   \begin{center}     \refstepcounter{algorithm}% New algorithm     \hrule height.8pt depth0pt \kern2pt% \@fs@pre for \@fs@ruled     \renewcommand{\caption}[2][\relax]{% Make a new \caption       {\raggedright\textbf{\ALG@name~\thealgorithm} ##2\par}%       \ifx\relax##1\relax % #1 is \relax         \addcontentsline{loa}{algorithm}{\protect\numberline{\thealgorithm}##2}%       \else % #1 is not \relax         \addcontentsline{loa}{algorithm}{\protect\numberline{\thealgorithm}##1}%       \fi       \kern2pt\hrule\kern2pt     }  }{% \end{breakablealgorithm}     \kern2pt\hrule\relax% \@fs@post for \@fs@ruled   \end{center}  }\makeatother
\begin{document}
\section{Some section}
\lipsum[1]
\begin{breakablealgorithm} \caption{Modeling ......} \label{alg:modeling} \begin{algorithmic}[1] \State Read the data file: 'Lorem Ipsum' \State I can eat glass, it does not hurt me \State I can eat glass, it does not hurt me \State I can eat glass, it does not hurt me \State I can eat glass, it does not hurt me \State I can eat glass, it does not hurt me \State I can eat glass, it does not hurt me \[ \text{Lorem Ipsum:} \quad x A (1 - \frac{1}{1}) - \alpha \cdot \text{Glass} \cdot A \] where: \[\text{Lorem Ipsum:} \quad x A (1 - \frac{1}{1}) - \alpha \cdot \text{Glass} \cdot A\] \State I can eat glass, it does not hurt meI can eat glass, it does not hurt meI can eat glass, it does not hurt me \State I can eat glass, it does not hurt meI can eat glass, it does not hurt meI can eat glass, it does not hurt me \State I can eat glass, it does not hurt me \[\text{Lorem Ipsum:} \quad x A (1 - \frac{1}{1}) - \alpha \cdot \text{Glass} \cdot A\] \State I can eat glass, it does not hurt meI can eat glass, it does not hurt meI can eat glass, it does not hurt me \State Output ........... \State As dawn broke over the horizon: \State \quad The adventurers gathered their supplies — maps, lanterns, and charms. \State \quad Each carried a secret token (a silver coin, a feather, and a stone). \State \quad The forest stretched endlessly, veiled in morning mist. \State \quad Time felt frozen, yet fleeting. \State Their guide, an old sage: \State \quad Spoke of paths less traveled and shadows that whispered. \State \quad Warned of the river crossing at twilight. \State \quad And the moonlit grove where choices would bind their fate. \State With resolve, they stepped onto the mossy trail: \State \quad Their steps matched an unspoken rhythm. \State \quad Echoes of distant bells resonated in the air. \State A sudden breeze carried faint laughter: \State \quad Was it the forest itself or something hidden within? \State \quad The youngest adventurer clutched their charm tightly. \For{every mile deeper into the woods} \State Shadows grew longer and whispers louder. \State A riddle carved into an ancient oak appeared: \[ \textit{"To find the light, embrace the dark; to move ahead, step apart."} \] \State They chose their steps carefully, navigating roots and stones: \[ \textit{"One misstep could wake the slumbering earth."} \] \State Night fell as the group reached the silver brook: \[ \textit{"Here lies the mirror of truth and deceit."} \] \State They paused, letting the silence settle before crossing. \EndFor \State As dawn approached once again: The forest opened to a clearing bathed in gold. \State Their journey, though incomplete, had reshaped them forever.\end{algorithmic}\end{breakablealgorithm}
I can cite it \ref{alg:modeling} here.
\end{document}

点击👇LaTeX工作室” 关注公众号
精致美好科研生活从 LaTeX 开始!



点击👇LaTeX工作室” 关注公众号
从 LaTeX 开始即刻享受科研精致美好生活
 关注公众号回复1进本硕博脱单群

成为 LaTeX 会员,尽享精致科研!

开通 LaTeX VIP 地址:

https://www.latexstudio.net/index/recharge/choice.html


LaTeX工作室
精致科研生活从 LaTeX 开始! 模版定制 | 培训 | 排版 | 答疑 加VX:t314159265
 最新文章