文本模式下的换行符不起作用

文本模式下的换行符不起作用

这是我之前的问题

我正在用 LaTeX 编写一份大型文档,其中的一部分让我很烦恼。

具体来说,我以几个编号步骤描述了一种算法。然后,我给出了该算法的伪代码,我想在其中引用这些步骤。

有时,伪代码的多行引用算法描述中的一个步骤。因此,我想画一个大的右括号 ( }) 将这些行括起来,并用步骤编号标记。

根据我从 LaTeX 文档中了解到的知识,我有以下(相关的)代码片段:

\documentclass[a4paper,11pt]{article}
\usepackage[pdftex]{graphicx}
\usepackage{amsmath}
\usepackage{amsfonts}
\usepackage{amssymb}
\usepackage{verbatim}
\newcommand{\HRule}{\rule{\linewidth}{0.2mm}}
\begin{document}
\title{Single Source Multi-Commodity Network Flow}
\date{December 18, 2011}
\author{Ashwin Panchapakesan \\
        University of Ottawa \\
        Student Number: 6588301 \\
        [email protected]}
\maketitle

\section{Pseudo-code}
.

function getNeighbor(V,E, paths, dist)

\indent \indent extern A

\indent \indent u $\leftarrow$ NULL

\indent \indent n $\leftarrow$ NULL

\indent \indent while (u = NULL) DO

\[
 \left.
 \text{ 
    us $\leftarrow$ \{ v $\in V | \exists$ t $\in$ T $\cap v, \newline$ \} 
    ($\exists$ e $\in E | v \in$ e, c(e) = $\infty$), \linebreak \linebreak
    $\indent \indent \indent \indent$ routable(u, paths) if $\exists$ u $\in V | (u,v) \in$ E else routable(u, paths) \linebreak
    $\indent \indent \indent \indent$ where (v,u) $\in$ E, w(e) $\leq$ dist \}
    $\indent \indent \indent \indent$ where (v,u) $\in$ E, w(e) $\leq$ dist \}
 }
 \right\rbrace step \ref{getListofNodes}
\]

\indent \indent \indent // step \ref{getListofNodes}

\indent \indent \indent $us \leftarrow \{ v \in V | \exists t \in T \cap v, $

\indent \indent \indent \indent ($\exists e \in E | v \in e, c(e) = \infty$), 

\indent \indent \indent \indent routable(u, paths) if $\exists u \in V | (u,v) \in E else$ routable(u, paths) 

\indent \indent \indent \indent where (v,u) $\in E, w(e) \leq dist \} $

\indent \indent \indent U $\leftarrow$ max$_{\sum_{e \in paths.values} w(e)}$ \{ paths \} // step \ref{setU}

\indent \indent \indent  // step \ref{setU} begins

\indent \indent \indent if (U $\in$ us) then

\indent \indent \indent \indent u $\leftarrow$ U

\indent \indent \indent else

\indent \indent \indent \indent u $\leftarrow$ randomChoice(us)

\indent \indent \indent // step \ref{setU} ends

\indent \indent // step \ref{setN} begins

\indent \indent while (n = NULL) do

\indent \indent \indent ns $\leftarrow \{ v \in V | u \neq v,$ \linebreak
\indent \indent \indent \indent $(\exists e \in E | e=(u,v) \text{ or } e=(v,u), c(e) = \infty),$ \linebreak
\indent \indent \indent \indent $min_{w(e)} {e \in E | e=(u,v) \text{ or } e=(v,u)} \leq dist\}$

\indent \indent n $\leftarrow$ randomChoice(ns)

\indent \indent // step \ref{setN} ends

\indent \indent // step \ref{setR} begins

\indent \indent ress $\leftarrow$ \{$ a_i \in A | t_i \in n$ \}

\indent \indent r $\leftarrow$ randomChoice(ress)

\indent \indent // step \ref{setR} ends

\indent \indent r $\leftarrow a_i$ for some $a_i \in A$


\indent \indent // step \ref{changeTerminal} begins

\indent \indent $t_i \leftarrow n$

\indent \indent $\forall e \in $ paths[r] do

\indent \indent \indent f(e) $\leftarrow$ f(e) - 1

\indent \indent // step \ref{changeTerminal} ends

\indent \indent // step \ref{changePath} beings \linebreak
\indent \indent p = min$_{\sum_{path}w(e)}$ \{ path $|$ path = paths[r], $\exists a_i \in A | r = a_i, t_i \in n$, \linebreak
\indent \indent \indent $\forall e \in paths[r] c(e)-f(e)>0$ \}

\indent \indent $\forall e \in p$ do

\indent \indent \indent \indent f(e) $\leftarrow$ f(e) + 1

\indent \indent paths[r] $\leftarrow$ paths[r] + min$_{w(e)}$ \{$ e \in E |e=(u,n) or e=(n,u) $ \}

\indent \indent // step \ref{changePath} ends

\end{document}

我的问题是\newlines 和\linebreaks 未显示在输出文档中 - 它们不会产生换行符。相反,所有预期的多行都显示在一行上,并在页边距处被截断。

另外,我知道\text{和之间}是文本环境,就像$和之间是数学环境一样。有人能解释一下和$内环境的名称吗?\[\]

我将非常感激任何关于如何解决此问题的帮助。

谢谢

答案1

\newline并且\linebreak在 内部没有效果\text。您应该考虑tabbing环境。这是伪代码的第一部分,可以以相同的方式完成:

\documentclass[a4paper,11pt]{article}
\usepackage{graphicx}
\usepackage{amsmath}
\usepackage{amsfonts}
\usepackage{amssymb}
\usepackage{verbatim}
\newcommand{\HRule}{\rule{\linewidth}{0.2mm}}
\begin{document}
\title{Single Source Multi-Commodity Network Flow}
\date{December 18, 2011}
\author{Ashwin Panchapakesan \\
        University of Ottawa \\
        Student Number: 6588301 \\
        [email protected]}
\newcommand{\vv}[1]{\mathit{#1}}
\maketitle

\section{Pseudo-code}

\begin{tabbing}
\hspace*{20pt}\=\hspace*{20pt}\=\hspace*{20pt}\=\kill
function $\vv{getNeighbor}(V, E, \vv{paths}, \vv{dist})$ \\
\> extern $A$ \\
\> $u\leftarrow \mathrm{NULL}$ \\
\> $n$ $\leftarrow \mathrm{NULL}$ \\
\> while ($u = \mathrm{NULL}$) DO \\
$\left.\kern-\nulldelimiterspace
\begin{minipage}{\textwidth}
\begin{tabbing}
\hspace*{20pt}\=\hspace*{20pt}\=\hspace*{20pt}\=\kill
\>\> $\vv{us}\leftarrow \{ v \in V \mid \exists t \in T \cap v$ \\
\>\>\> ($\exists e\in E \mid v \in e, c(e) = \infty$), \\
\>\>\> $\vv{routable}(u, \vv{paths})$ if $\exists u \in V \mid (u,v) \in E$ else $\vv{routable}(u,\vv{paths}$) \\
\>\>\> where $(v,u) \in E$, $w(e) \leq \vv{dist}$ \\
\>\>\> where $(v,u) \in E$, $w(e) \leq \vv{dist}\}$
\end{tabbing}
\end{minipage}
\right\rbrace\text{step \ref{getListofNodes}}$ \\
\>\> // step \ref{getListofNodes} \\

\>\> $\vv{us}\leftarrow \{ v \in V \mid \exists t \in T \cap v,$ \\
\>\>\> ($\exists e \in E \mid v \in e, c(e) = \infty$), \\
\>\>\> $\vv{routable}(u, \vv{paths})$ if $\exists u \in V \mid (u,v) \in E$ else $\vv{routable}(u,\vv{paths})$ \\
\>\>\> where $(v,u)\in E$, $w(e) \leq dist \}$\\
\>\> $U\leftarrow\max_{\sum_{e \in \vv{paths.values}} w(e)} \{ \vv{paths} \}$ // step \ref{setU} \\
\>\> // step \ref{setU} begins \\
\>\> if ($U \in \vv{us}$) then \\
\>\>\> $u\leftarrow U$ \\
\>\> else \\
\>\>\> $u\leftarrow \vv{randomChoice}(\vv{us})$ \\
\>\> // step \ref{setU} ends \\

[...]

\end{tabbing}
\end{document}

在此处输入图片描述

宽括号有点棘手:它tabbing在 a 中使用嵌套环境,利用仅包含 a 的 a具有必要的宽度minipage这一事实。minipagetabbing

相关内容