如何在Latex中的新命令中添加空行

6
我正在使用以下代码,其中使用了 \newcommand(宏):
\documentclass[12pt]{article}

\usepackage{color}
\usepackage{graphicx}

\begin{document}

% definition of new commands: 
\newcommand{\mytitle}[1]{\LARGE\color{black}\centering\textbf{#1\newline}}   
        % NEWLINE WORKS IN ABOVE LINE
\newcommand{\mycode}{\small\color{green}Code:\scriptsize\leftskip30pt\color{red}\\*}%   
        % CANNOT ADD NEWLINE BEFORE "Code:" IN ABOVE LINE
\newcommand{\mytext}{\normalsize\color{black}\leftskip0pt}
\newcommand{\myoutput}{\small\color{green}Output:\scriptsize\leftskip30pt\color{blue}\\}%   
        % CANNOT ADD NEWLINE BEFORE "Output:" IN ABOVE LINE

\quote   % to prevent indentation of first line; 

% main text starts here: 

\mytitle{Simple text, code and figure.}

\mytext
This is normal text- part 1.\\ 
This is normal text- part 1.\\ 
This is normal text- part 1.\\ 

\mycode
This is code line 1.\\ 
This is code line 2.\\ 
This is code line 3.\\ 

\myoutput
This is output line 1.\\ 
This is output line 2.\\ 
This is output line 3.\\ 

\mytext
This is normal text- part 2.\\ 
This is normal text- part 2.\\ 
This is normal text- part 2.\\ 

\mycode
This is code part 2.\\ 
This is code part 2.\\ 
This is code part 2.\\ 

\myoutput
This is output part 2.\\ 
This is output part 2.\\ 
This is output part 2.\\ 

\mytext
The last line in normal text.\\ 

\end{document}

尽管输出结果是正确的,但我无法在“Code:”、“Output”和文字部分之前插入空行:

enter image description here

我尝试使用\\\newline\linebreak[1]在箭头标记的位置添加空行,但它们都会产生以下错误:

There's no line here to end.

我该如何在文本的代码、输出和文本部分之前添加空行?感谢您的帮助。

1个回答

16

此处所述,您可以使用以下命令之一,具体取决于要跳过多少空格:

  • \smallskip
  • \medskip
  • \bigskip
  • 或更灵活的命令:\vspace{length-of-space}

现在来看看您尝试了什么。
在这里使用\\\newline\linebreak是否可能?

是的,但是需要在这些命令之前加上某些内容,因为正如错误消息所说,没有要结束的行。您可以在这些命令之前使用不间断空格~

因此,以下所有内容也可行:

  • ~\\
  • ~\newline
  • ~\linebreak

另一个相关的帖子:长度及其使用时机


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接