2

I am trying to compile the following code and trying to get spaces at start of code using spacing commands \hspace{2em}, \qquad and \indent. But space does not show up instead all things are being printed as normal lines. I am working with PDFLaTeX.

\documentclass[11pt]{article}

\begin{document}

\begin{enumerate}

\item Initialize beg=0 and end=length-1.

\item Repeat step 8 till low  high. 

\item Middle =(low + high) / 2. 
%%%%%%% I need Space Hereafter %%%%%%%

\item if searcharray[middle]=searchno  \\
\indent\indent Search is successful  \\
\indent return middle.  \\
else if searcharray[middle]$>$searchno[end]  \\
\indent end=middle - 1  \\
else  \\
\indent beg=middle + 1. \\

\end{enumerate}

\end{document}
  • 1
    Welcome to TeX.SX! It seems to me you want to write down an algorithm in pseudo code. Instead of using enumerate, you can have a look at the wonderful packages for this purpose https://en.wikibooks.org/wiki/LaTeX/Algorithms, Write pseudo code in latex and Writing an algorithm in LaTeX. If it is source code you are writing, consider https://en.wikibooks.org/wiki/LaTeX/Source_Code_Listings and http://texblog.org/2008/04/02/include-source-code-in-latex-with-listings/. – moewe Aug 22 '15 at 14:26
  • That solves this particular problem. Fine. The problem of spacing however remains, space is not there at start of output even if \indent is used again and again. – Soumyajit Aug 22 '15 at 15:29
  • \indent is just not how you indent with the algo packages. The indentation is done by nesting. Please update your question with what you use now (what pacakge did you end up using?), and make what you want to acheive a bit more precise (I'm guessing you want horizontal indentation, but am not too sure.). See also Indenting lines of code in algorithm, Change Indentation Size in algorithmicx package. – moewe Aug 22 '15 at 15:31
  • @SoumyajitSamanta I've provided an answer explaining why \indent doesn't produce any indentation inside a list and also offering some advise on typesetting pseudo-code. – Gonzalo Medina Aug 22 '15 at 17:58
  • you can try with \hspace*{2em} – touhami Aug 22 '15 at 18:01

1 Answers1

2

Explanation

\indent basically switches to horizontal mode and inserts a box of width \parindent; since inside a list (such as enumerate) \parindent is 0pt, using \indent inside a list won't produce any indentation.

Recommendation

You can define a command to give you the desired indentation:

\documentclass{article}

\newcommand\Myindent[1][15pt]{\hspace*{#1}}

\begin{document}

\begin{enumerate}
\item Test

\indent\indent No indentation.

\Myindent\Myindent Indentation.
\end{enumerate}

\end{document}

enter image description here

Notice the use of the starred version \hspace*; using \hspace won't work because \hspace is ignored at the beginning of lines.

However, you shouldn't need to use manual \indents or spaces in your documents. If you want to write pseudo-code with indentation, you should consider using one of several dedicated packages; two of the more popular ones are:

Gonzalo Medina
  • 505,128