Discussion:
algorithm2e Indenting a paragraph of text in algorithm
(too old to reply)
a***@hotmail.co.uk
2017-12-09 13:49:29 UTC
Permalink
Here below is my MWE:
Comping the below will show that the big paragraph of text is not in line with the first word. In other words it look like the paragraph is indented, when in fact my goal is to make the paragraph appear like this one (i.e each line begins without indentation). instead of:

" Comping the below will show that the big paragraph of text is not in line with the first word, in other words making it look like the paragraph is indented, when in fact my goal is to make the paragraph appear like this one (each line begins without indentation). instead of"

I'm pretty sure its a quick fix, any ideas?

%-------------------------------------------------------------
\documentclass[12pt, oneside]{book}
\usepackage{lipsum} %remove \lipsum[#]

%ALGORITHM CODE
\usepackage[linesnumbered,ruled]{algorithm2e}


%-----------------------------BEGIN-----------------------------
\begin{document}

\begin{algorithm}[H]
\SetKwInOut{Input}{Input}
\SetKwInOut{Output}{Output}
\SetKwInOut{Define}{Define}

\underline{Initialise:} Conditions for Algorithm\;
\Define{define functions for algorithm}
\Input{input of algoithm}
\Output{Output}

\underline{Run through algorithm In Full:}

{

\hspace{5pt} \textbf{Start}:

}
{

\hspace{15pt} \lipsum[1]

}
{

\hspace {5pt} \textbf{End}

}

\caption{Name of algoithm and details etc...}
\end{algorithm}


\end{document}

%-------------------------------------------------------------
Michael Shell
2017-12-09 18:19:34 UTC
Permalink
On Sat, 9 Dec 2017 05:49:29 -0800 (PST)
Post by a***@hotmail.co.uk
Comping the below will show that the big paragraph of text is not
in line with the first word.
If I understand correctly, I see two issues.

1. Watch out for an added space after the end brace of \hspace{}.
You can add a trailing \relax to gobble that.
2. Here you probably want to use \leftskip rather than \hspace
for the paragraph.

e.g., something like:


{

\hspace{5pt}\relax \textbf{Start}:

}
{

\leftskip 10pt\relax \lipsum[1]

}
{

\hspace {5pt}\relax \textbf{End}

}


or (cleaner):

\bgroup
\parindent 0pt
\leftskip 5pt

\textbf{Start}:

\leftskip 10pt\relax \lipsum[1]

\textbf{End}

\egroup


I would also read the algorithm2e manual to make sure there is not
some option, macro or setup parameter where you won't have to manually
set those spaces in all the algorithms of your document.
(Or create your own environment or macro to define that spacing.)


Cheers,

Mike Shell
Michael Shell
2017-12-09 18:45:29 UTC
Permalink
On Sat, 9 Dec 2017 13:19:34 -0500
Post by Michael Shell
\leftskip 10pt\relax \lipsum[1]
Errr, better make that:


\bgroup
\parindent 0pt
\leftskip 5pt

\textbf{Start}:

\bgroup\leftskip 10pt\relax \lipsum[1]\par\egroup

\textbf{End}

\egroup



lest the "End" lines up with the paragraph text rather than
the "Start".


Cheers,

Mike
a***@hotmail.co.uk
2017-12-09 19:56:24 UTC
Permalink
Thank you for your reply. The only thing i wish to comment on is that, using an example paragraph. the aliment of the text that i am looking for is

This:
"Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Ut puruselit, vestibulum ut, placerat ac, adipiscing vitae, felis. Curabiturdictum gravida mauris. Nam arcu libero, nonummy eget" (All the text begins on the same line)

However your solution gives the alignment of text where the subsequent lines starts after the first word i.e. your solution gives:

"Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Ut puruselit,
vestibulum ut, placerat ac, adipiscing vitae, felis. Curabiturdictum
gravida mauris. Nam arcu libero, nonummy eget" (All the text begins on the
same line)"
Athel Cornish-Bowden
2017-12-10 08:56:06 UTC
Permalink
Post by a***@hotmail.co.uk
Thank you for your reply. The only thing i wish to comment on is that,
using an example paragraph. the aliment of the text that i am looking
for is
"Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Ut
puruselit, vestibulum ut, placerat ac, adipiscing vitae, felis.
Curabiturdictum gravida mauris. Nam arcu libero, nonummy eget" (All
the text begins on the same line)
When I want something like (which is often) I put \noindent at the
beginning of the paragraph. It's very simple. Does it not give what you
want?
Post by a***@hotmail.co.uk
However your solution gives the alignment of text where the subsequent
"Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Ut puruselit,
vestibulum ut, placerat ac, adipiscing vitae, felis. Curabiturdictum
gravida mauris. Nam arcu libero, nonummy eget" (All the text begins on the
same line)"
--
athel
Athel Cornish-Bowden
2017-12-10 11:08:17 UTC
Permalink
Post by Athel Cornish-Bowden
Post by a***@hotmail.co.uk
Thank you for your reply. The only thing i wish to comment on is that,
using an example paragraph. the aliment of the text that i am looking
for is
"Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Ut
puruselit, vestibulum ut, placerat ac, adipiscing vitae, felis.
Curabiturdictum gravida mauris. Nam arcu libero, nonummy eget" (All
the text begins on the same line)
When I want something like (which is often) I put \noindent at the
beginning of the paragraph. It's very simple. Does it not give what you
want?
Looking again at your subject line I think that maybe you want the
whole paragraph indented, but the first line no more or less than the
others. That is also something I often want, and I put the paragraph as
a quotation:

\begin{quotation}
\noindent Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Ut
puruselit, vestibulum ut, placerat ac, adipiscing vitae, felis.
Curabiturdictum gravida mauris. Nam arcu libero, nonummy eget
\end{quotation}
Post by Athel Cornish-Bowden
Post by a***@hotmail.co.uk
However your solution gives the alignment of text where the subsequent
"Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Ut puruselit,
vestibulum ut, placerat ac, adipiscing vitae, felis. Curabiturdictum
gravida mauris. Nam arcu libero, nonummy eget" (All the text begins on the
same line)"
--
athel
a***@hotmail.co.uk
2017-12-09 19:57:06 UTC
Permalink
Any ideas how to correct this?
a***@hotmail.co.uk
2017-12-09 19:59:06 UTC
Permalink
Thank you for your reply. The only thing i wish to comment on is that, using an example paragraph. the aliment of the text that i am looking for is

This:
"Lorem ipsum dolor sit amet, consectetuer adipiscing elit.
Ut puruselit, vestibulum ut, placerat ac, adipiscing vitae,
felis. Curabiturdictum gravida mauris. Nam arcu libero,
nonummy eget"

(All the text begins on the same line)

However your solution gives the alignment of text where the subsequent lines starts after the first word i.e. your solution gives:

"Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Ut puruselit,
vestibulum ut, placerat ac, adipiscing vitae, felis. Curabiturdictum
gravida mauris. Nam arcu libero, nonummy eget" (All the text begins on the
same line)"

Any ideas on how to correct this?
a***@hotmail.co.uk
2017-12-09 20:01:00 UTC
Permalink
Thank you for your reply. The only thing i wish to comment on is that, using an example paragraph. the aliment of the text that i am looking for is

This:
"Lorem ipsum dolor sit amet, consectetuer adipiscing elit.
Ut puruselit, vestibulum ut, placerat ac, adipiscing vitae,
felis. Curabiturdictum gravida mauris. Nam arcu libero,
nonummy eget"

(i.e. All the text begins on the same line)

However your solution gives the alignment of text where the subsequent lines starts after the first word i.e. your solution gives:

"Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Ut puruselit,
vestibulum ut, placerat ac, adipiscing vitae, felis. Curabiturdictum
gravida mauris. Nam arcu libero, nonummy eget" (All the text begins on the
same line)"

Any ideas on how to correct this.
Michael Shell
2017-12-10 12:05:39 UTC
Permalink
On Sat, 9 Dec 2017 12:01:00 -0800 (PST)
Post by a***@hotmail.co.uk
However your solution gives the alignment of text where the subsequent
lines starts after the first word
This could be a package version difference issue.
On my system, this code:



\documentclass[12pt, oneside]{book}
\usepackage{lipsum} %remove \lipsum[#]

%ALGORITHM CODE
\usepackage[linesnumbered,ruled]{algorithm2e}


%-----------------------------BEGIN-----------------------------
\begin{document}

\begin{algorithm}[H]
\SetKwInOut{Input}{Input}
\SetKwInOut{Output}{Output}
\SetKwInOut{Define}{Define}

\underline{Initialise:} Conditions for Algorithm\;
\Define{define functions for algorithm}
\Input{input of algoithm}
\Output{Output}

\underline{Run through algorithm In Full:}


\bgroup
\parindent 0pt
\leftskip 5pt

\textbf{Start}:

\bgroup\leftskip 5pt\relax \lipsum[1]\par\egroup

\textbf{End}

\egroup

\caption{Name of algoithm and details etc...}
\end{algorithm}
\end{document}



generates a paragraph in which all the lines are left aligned,
as a block, aligned with the words Start and End. The paragraph's
very first line is aligned the same way as the later ones.

What does the above code do on your system?


Cheers,

Mike
a***@hotmail.co.uk
2017-12-10 12:24:35 UTC
Permalink
Unfortunately this gives the same response as your original attempt where the all subsequent lines following the first is shifted slightly to the left.

Athel's solution provides what i'm aiming for. i.e The whole paragraph indented, but the first line no more or less than the others.

So i am using the following as my staring point:

\begin{algorithm}[H]
\SetKwInOut{Input}{Input}
\SetKwInOut{Output}{Output}
\SetKwInOut{Define}{Define}

\underline{Initialise:} Conditions for Algorithm\;
\Define{define functions for algorithm}
\Input{input of algoithm}
\Output{Output}

\underline{Run through algorithm In Full:}

{

\hspace{5pt}\relax \textbf{Start}:

}
{

\begin{quotation}
\noindent \lipsum[1]
\end{quotation}

}
{

\hspace {5pt}\relax \textbf{End}

}


\caption{Name of algoithm and details etc...}
\end{algorithm}


Currently the only issue i find with this solution is that is is shifted a bit too much the left by approximately 10pt , and am currently experimenting on a way to manually shift it right a bit. Any ideas?
a***@hotmail.co.uk
2017-12-10 12:42:55 UTC
Permalink
On Further investigation i have found the following modification to your solution has work perfectly for my needs:

\begin{algorithm}[H]
\SetKwInOut{Input}{Input}
\SetKwInOut{Output}{Output}
\SetKwInOut{Define}{Define}

\underline{Initialise:} Conditions for Algorithm\;
\Define{define functions for algorithm}
\Input{input of algoithm}
\Output{Output}

\underline{Run through algorithm In Full:}


\bgroup
\parindent 5pt
\leftskip 5pt

\textbf{Start}:

\bgroup\leftskip 15pt\relax \lipsum[1]\par\egroup

\textbf{End}

\egroup

\caption{Name of algoithm and details etc...}
\end{algorithm}


\end{document}


Now simply for the sake of my own OCD i am trying to put this in the "non-compact" form i.e in the same syntax as my original, so that i do not have to go back and change all of my previous algorithms. Any ideas?
a***@hotmail.co.uk
2017-12-10 12:47:57 UTC
Permalink
i.e. something in the form


\begin{algorithm}[H]
\SetKwInOut{Input}{Input}
\SetKwInOut{Output}{Output}
\SetKwInOut{Define}{Define}

\underline{Initialise:} Conditions for Algorithm\;
\Define{define functions for algorithm}
\Input{input of algoithm}
\Output{Output}

\underline{Run through algorithm In Full:}

{

\hspace{5pt} \textbf{Start}:

}
{

INSERT CORRECT MODIFICATION HERE

}
{

\hspace {5pt} \textbf{End}

}

Again just for my own OCD purposes, which is the form that i prefer if in the future i need to go back and modify my previous algorithms.
a***@hotmail.co.uk
2017-12-10 12:59:59 UTC
Permalink
I see now why the modification i suggested to your code set of my OCD, it is because the numbering has also been shifted to the left. So i am trying to non-compact it....
Michael Shell
2017-12-10 16:37:49 UTC
Permalink
On Sun, 10 Dec 2017 04:59:59 -0800 (PST)
Post by a***@hotmail.co.uk
I see now why the modification i suggested to your code set of
my OCD, it is because the numbering has also been shifted
to the left. So i am trying to non-compact it....
{ corresponds to \bgroup
} corresponds to \egroup

However, the problem with using braces is that doing
constructs like

{a}
{b}

introduce, often unexpected and unwanted, spaces between the
a and b. The \bgroup \egroup forms don't have this problem.

Anyway, the left shift of the numbers I think is due to
algorithm2e's patching into the paragraph system.

In any case, it is best you use a macro to define such
special contructs so you only have one place to change
it as need be.

Try this and see how you like it:



\documentclass[12pt, oneside]{book}
\usepackage{lipsum} %remove \lipsum[#]

%ALGORITHM CODE
\usepackage[linesnumbered,ruled]{algorithm2e}


%-----------------------------BEGIN-----------------------------
\begin{document}

\newcommand{\MYalgopar}[1]{\bgroup
\parindent 0pt
\leftskip 5pt
\par\textbf{Start}:\par
\bgroup\everypar={\nl\hspace{5pt}\relax}\leftskip 15pt\relax
#1\egroup\textbf{End}\par\egroup}

\begin{algorithm}[H]
\SetKwInOut{Input}{Input}
\SetKwInOut{Output}{Output}
\SetKwInOut{Define}{Define}

\underline{Initialise:} Conditions for Algorithm\;
\Define{define functions for algorithm}
\Input{input of algoithm}
\Output{Output}

\underline{Run through algorithm In Full:}

\MYalgopar{\lipsum[1]}

\caption{Name of algoithm and details etc...}
\end{algorithm}


\end{document}


It will handle multiple paragraphs as well:

\MYalgopar{\lipsum[1]\par\lipsum[1]}



Cheers,

Mike

Loading...