我试图将算法中的长行(即我的 if 条件中的行)拆分为两行,同时保持缩进。并且我不希望换行符出现在代码中除我的 -or- 之后的任何地方。
这是我的代码
\documentclass[a4paper,10pt]{article}
\usepackage[utf8]{inputenc}
\usepackage{algorithm}
\usepackage{algpseudocode}
\algnewcommand\algorithmicor{\textbf{or}}
\newcommand{\pushcode}[1][1]{\hskip\dimexpr#1\algorithmicindent\relax}
\begin{algorithm}[h]
\caption{Core genome identification}
\begin{algorithmic}[1]
%I tired using this push
\While{$list$ is not empty \algorithmicor \\ \pushcode[1] second condition}
\EndWhile
\end{algorithmic}
\end{algorithm}
\end{document}
例如,我已经尝试过这次推送,但是这给了我一个我不想要的新行号!
其他建议,例如定义
\newcommand{\myindent}[1]{
\newline\makebox[#1]{}
}
还将缩进放在特定位置。缩进不是我要搜索的内容。我希望它与之前的条件具有相同的缩进。因此,类似
if (this is my first condition OR
this is the second)
答案1
重新定义环形和如果序言部分(之后\usepackage{algpseudocode}
)的声明如下:
\newcommand\CONDITION[2]%
{\begin{tabular}[t]{@{}l@{}}#1#2\end{tabular}}
\algdef{SE}[WHILE]{While}{EndWhile}[1]%
{\algorithmicwhile\ \CONDITION{#1}{\ \algorithmicdo}}%
{\algorithmicend\ \algorithmicwhile}
\algdef{SE}[FOR]{For}{EndFor}[1]%
{\algorithmicfor\ \CONDITION{#1}{\ \algorithmicdo}}%
{\algorithmicend\ \algorithmicfor}
\algdef{S}[FOR]{ForAll}[1]%
{\algorithmicforall\ \CONDITION{#1}{\ \algorithmicdo}}
\algdef{SE}[REPEAT]{Repeat}{Until}%
{\algorithmicrepeat}[1]%
{\algorithmicuntil\ \CONDITION{#1}{}}
\algdef{SE}[IF]{If}{EndIf}[1]%
{\algorithmicif\ \CONDITION{#1}{\ \algorithmicthen}}%
{\algorithmicend\ \algorithmicif}
\algdef{C}[IF]{IF}{ElsIf}[1]%
{\algorithmicelse\ \algorithmicif\ \CONDITION{#1}{\ \algorithmicthen}}
\makeatletter
\ifthenelse{\equal{\ALG@noend}{t}}%
{\algtext*{EndWhile}%
\algtext*{EndFor}%
\algtext*{EndLoop}%
\algtext*{EndIf}%
}{}%
\makeatother
这些定义取自样式文件algpseudocode.sty
,并经过修改以处理多行条件。
这是一个完整的例子:
\documentclass{article}
\usepackage{algpseudocode}
\newcommand\CONDITION[2]%
{\begin{tabular}[t]{@{}l@{}}#1#2\end{tabular}}
\algdef{SE}[WHILE]{While}{EndWhile}[1]%
{\algorithmicwhile\ \CONDITION{#1}{\ \algorithmicdo}}%
{\algorithmicend\ \algorithmicwhile}
\algdef{SE}[FOR]{For}{EndFor}[1]%
{\algorithmicfor\ \CONDITION{#1}{\ \algorithmicdo}}%
{\algorithmicend\ \algorithmicfor}
\algdef{S}[FOR]{ForAll}[1]%
{\algorithmicforall\ \CONDITION{#1}{\ \algorithmicdo}}
\algdef{SE}[REPEAT]{Repeat}{Until}%
{\algorithmicrepeat}[1]%
{\algorithmicuntil\ \CONDITION{#1}{}}
\algdef{SE}[IF]{If}{EndIf}[1]%
{\algorithmicif\ \CONDITION{#1}{\ \algorithmicthen}}%
{\algorithmicend\ \algorithmicif}
\algdef{C}[IF]{IF}{ElsIf}[1]%
{\algorithmicelse\ \algorithmicif\ \CONDITION{#1}{\ \algorithmicthen}}
\makeatletter
\ifthenelse{\equal{\ALG@noend}{t}}%
{\algtext*{EndWhile}%
\algtext*{EndFor}%
\algtext*{EndLoop}%
\algtext*{EndIf}%
}{}%
\makeatother
\begin{document}
\begin{algorithmic}[1]
\While{short condition}
\EndWhile
\While{very long condition\\broken into two lines}
\EndWhile
\end{algorithmic}
\end{document}