Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

I'm using the algorithmicx package and would like to fully left justify the text when not using line numbers. Here's an example:

enter image description here

Notice how only the Require and Ensure clauses are fully pushed to the left. How can I make the rest of the code be fully left justified as well?

Here's a minimal example:

\documentclass[a4paper,11pt]{article}

\usepackage{algorithmicx}
\usepackage{algorithm}
\usepackage{algpseudocode}
\usepackage{amsmath}

\begin{document}

\begin{algorithm}
\caption{Algorithm Name}
\begin{algorithmic}
\Require some input
\Ensure some output
\Statex
\State \textbf{something}
\State more text
\begin{enumerate}
    \item list item 1
    \item an equation
        \begin{equation*}
            f(n) = \begin{cases}
            1 & i = 1\\
            f(n-1) + f(n-2) & i > 1
                  \end{cases}
        \end{equation*} 
    \item third item 
\end{enumerate}  
\State last line
\end{algorithmic}
\end{algorithm}

\end{document}
share|improve this question
1  
It depends of the environment enumerate. I recommend the package enumitem and than use \begin{enumerate}[leftmargin=0pt] –  Marco Daniel May 2 '12 at 20:23
    
Thanks for the suggestion. The enumerate part was not really my main focus, i.e. I just added it for this example. See also my comment to @Werner's answer. –  hakoja May 2 '12 at 21:14

1 Answer 1

up vote 4 down vote accepted

You should consider using the functionality of algorithmicx to produce your formatting requirements, rather than using the algorithmic environment merely as a shell for regular document elements.

Regarldess, this may be what you're after (using Marco's suggestion to use enumitem):

enter image description here

\documentclass[a4paper,11pt]{article}
\setlength{\parindent}{0pt}% Just for this example
\usepackage{algorithm}% http://ctan.org/pkg/algorithms
\usepackage{algpseudocode}% http://ctan.org/pkg/algorithmicx
\usepackage{amsmath}% http://ctan.org/pkg/amsmath
\usepackage{enumitem}% http://ctan.org/pkg/enumitem
\makeatletter
\def\algbackskip{\hskip\dimexpr-\algorithmicindent+\labelsep}
\def\LState{\State \algbackskip}%
\makeatother
\begin{document}

\begin{algorithm}
  \caption{Algorithm Name}
  \begin{algorithmic}
    \Require some input
    \Ensure some output
  \Statex
  \LState \textbf{something}
  \LState more text
  \begin{enumerate}[leftmargin=0pt,itemindent=1.5em]
    \item list item 1
    \item an equation
        \begin{equation*}
            f(n) = \begin{cases}
            1 & i = 1\\
            f(n-1) + f(n-2) & i > 1
                  \end{cases}
        \end{equation*} 
    \item third item 
  \end{enumerate}  
  \LState last line
\end{algorithmic}
\end{algorithm}

\textbf{something}\par
more text
\begin{enumerate}
    \item list item 1
    \item an equation
        \begin{equation*}
            f(n) = \begin{cases}
            1 & i = 1\\
            f(n-1) + f(n-2) & i > 1
                  \end{cases}
        \end{equation*} 
    \item third item 
\end{enumerate}  
last line
\end{document}

I've defined \LState which represents a left-aligned \State. Not sure how effective this would be if you have nested content within an \LState (haven't tested it yet).

The upper algorithm shows the required output, while the lower "algorithm" shows the same content set in regular text (for comparison purposes). There is slight horizontal adjustment in the displayed equation.

share|improve this answer
    
Thanks for the suggestion, this was just what I was looking for. W.r.t to the enumerate part: that was just something I added for the sake of this example and was not really meant to be of any significant importance for my question. So the point of using the algorithm env. is not for the sake of formatting, but because I actually need to present something as an algorithm (with numbers, captions etc..). Do you have a better suggestion for what I might use? –  hakoja May 2 '12 at 21:11
    
@hakoja: The algorithmic environment suits that, for sure. It allows for line numbering (and referencing of them), captions to entire algorithms (like the float algorithm environment (supplied by the algorithms bundle). It is even possible to include algorithm2e-style "connecting loop lines" (see Is it possible to have connecting loop lines (like algorithm2e) in algorithmic?). Many customizations are possible, but it depends on your specific requirements. –  Werner May 2 '12 at 21:24
    
Argh, tried it out now, but unfortunately there's a small problem: For long sentences that wrap to the next line (and any other sentence following this within the same LState) the part of the sentence which is wrapped to the next line is indented and not left justified. How may I fix this? –  hakoja May 2 '12 at 21:26
    
@hakoja: I'll figure out a way to correct this later (at work now). –  Werner May 2 '12 at 21:37
    
Thanks for your dedication, it's much appreciated. Although it just occurred to me that it's not really the formatting of algorithmicx I really depend on here! All I need is actually just the algorithm environment. I can just use normal text inside that environment of course, not using algorithmicx at all! I'm so stupid! :( sorry for wasting your time. –  hakoja May 2 '12 at 21:44

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.