由于浮动体环境不能跨页,因此在排版算法时,过长的代码块会被分隔不同的页面上。Tabular可通过Longtable
宏进行跨页排版,图片则可以缩放大小或者运用子图,那么,算法块是否有对应的解决方案呢?
1.Algorithm.tex
\documentclass{article}
\usepackage{algorithm}
\usepackage{algorithmic}
\usepackage{amsmath}
\begin{document}
\section{Algorithm Aapted}
\begin{algorithm}[h]
\caption{Modeling ......}
\label{algori:1}
\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 meI 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 ...........
\end{algorithmic}
\end{algorithm}
\begin{algorithm}[h]
\caption{The Journey Through the Enchanted Forest}
\label{algori:2}
\begin{algorithmic}[1]
\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{algorithm}
\end{document}
这是我遇到的困难,我设法保持了代码块的长度,但是混淆了内容
2.一键下载
Algorithm_mwe.tex
不同的算法实现宏包的syntax
有些许差异,建议参考这个链接
浅浅修改了一下:
\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}
闪电侠
@u115617 群里讨论过类似的...