Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add blank lines in new commands in Latex

Tags:

macros

latex

I am using following code which uses \newcommand (macro):

\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}

Though the output is all right, I am not able to enter blank lines before "Code:", "Output" and text parts:

enter image description here

I have tried to use \\ \newline and \linebreak[1] to add blank lines at points marked by arrows, but they all produce following error:

There's no line here to end.

How can I add blank lines before code, output and text parts of text? Thanks for your help.

like image 814
rnso Avatar asked Dec 19 '22 07:12

rnso


1 Answers

As documented here, you can use one of the following commands depending on how much space you want to skip:

  • \smallskip
  • \medskip
  • \bigskip
  • or the more flexible one: \vspace{length-of-space}

Now coming to what you have tried.
Is it possible to use \\, \newline and \linebreak here?

Yes but you need something before those commands since, as the error message says, there is no line to end.
You can use a non-breaking space, ~, before those commands.

So all of the following would also work:

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

Another relevant post: Lengths and when to use them

like image 184
Sardar Usama Avatar answered Feb 21 '23 05:02

Sardar Usama