我有以下代码
\DeclareCaptionLabelFormat{algocaption}{Algorithm} % defines a new caption label as Algorithm x.y
\lstnewenvironment{algorithm}[1][
{
\captionsetup
{
justification=raggedright,
singlelinecheck=false,
labelformat=algocaption
}
\lstset
{
mathescape=true,
escapeinside={}, % I think I need to change this
literate={\\\\[0pt]}{{}}1,
breaklines=false,
frame=tB,
basicstyle=\scriptsize,
keywordstyle=\color{black}\bfseries,
keywords=
{
,input,
output,
return,
datatype,
function,
in,
if,
else,
foreach,
while,
begin,
end,
}
xleftmargin=0.4\textwidth,
#1
}
}{}
应该转向
\begin{algorithm}
input: int N, int D\\[0pt]
output: int\\[0pt]
begin\\[0pt]
res $\gets$ 0\\[0pt] %% I manually edited this in overleaf
while N \(\geq\) D\\[0pt]
N \(\gets\) N - D\\[0pt]
res \(\gets\) res + 1\\[0pt]
end\\[0pt]
return res\\[0pt]
end\\[0pt]
\end{algorithm}
进入以下...
但乳胶却给了我......
我知道我可以使用$$
来获得所需的结果,但这不是我想要做的(从 emacs org 模式导出后,这会自动格式化,据我所知,行为无法更改)。此外,我知道我可以添加''
但escapeinside
我真的不想添加任何额外的字符。似乎应该可以将分隔符内的转义设置为 和\\(
,\\)
但尝试这样做不起作用。
平均能量损失
\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage{color}
\usepackage{listings}
\usepackage{caption}
\DeclareCaptionLabelFormat{algocaption}{Algorithm} % defines a new caption label as Algorithm x.y
\lstnewenvironment{algorithm}[1][]{\captionsetup{justification=raggedright,singlelinecheck=false,labelformat=algocaption}\lstset{mathescape=true, escapeinside={}, literate={\\\\[0pt]}{{}}1, breaklines=false, frame=tB,basicstyle=\scriptsize, keywordstyle=\color{black}\bfseries,keywords={,input, output, return, datatype, function, in, if, else, foreach, while, begin, end, } xleftmargin=0.4\textwidth,#1}}{}
\begin{document}
%% Problematic
\begin{algorithm}
input: int N, int D\\[0pt]
output: int\\[0pt]
begin\\[0pt]
res \(\gets\) 0\\[0pt]
while N \(\geq\) D\\[0pt]
N \(\gets\) N - D\\[0pt]
res \(\gets\) res + 1\\[0pt]
end\\[0pt]
return res\\[0pt]
end\\[0pt]
\end{algorithm}
%% Desired
\begin{algorithm}
input: int N, int D\\[0pt]
output: int\\[0pt]
begin\\[0pt]
res $\gets$ 0\\[0pt]
while N $\geq$ D\\[0pt]
N $\gets$ N - D\\[0pt]
res $\gets$ res + 1\\[0pt]
end\\[0pt]
return res\\[0pt]
end\\[0pt]
\end{algorithm}
\end{document}