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 would like to save some horizontal space by changing indent space for package algpseudocode. How can I do that?

I think the variable I have to change is \algorithmicindent but I can't manage to do that.

\documentclass{article}
\usepackage{algorithm}% http://ctan.org/pkg/algorithm
\usepackage{algpseudocode}% http://ctan.org/pkg/algorithmicx

\usepackage{caption}
\usepackage{subcaption}
\usepackage{tikz}
\begin{document}
\section{foo}
Text

\setlength\fboxsep{0pt}
\renewcommand\algorithmicindent{.5em}

\begin{figure}[!ht]
  \fbox{
  \begin{subfigure}[b]{.5\linewidth}
    \centering
    \begin{algorithmic}[0]
      \Procedure{Euclid}{$a,b$}\Comment{The g.c.d. of a and b}
        \State $r\gets a\bmod b$
        \While{$r\not=0$}\Comment{We have the answer if r is 0}
          \State $a\gets b$
          \State $b\gets r$
          \State $r\gets a\bmod b$
        \EndWhile\label{euclidendwhile}
        \State \textbf{return} $b$\Comment{The gcd is b}
      \EndProcedure
    \end{algorithmic}

    \caption{Algorithm}\label{fig:alg}
  \end{subfigure}%
  }
  \quad
  \begin{subfigure}[b]{.5\linewidth}
    \centering
    \tikz\draw [fill=red!20] (0,0) rectangle (3,2);
    \caption{tikz}\label{fig:tikz}
  \end{subfigure}
  \caption{Both}\label{fig:both}
\end{figure}

Text \ref{fig:alg} and \ref{fig:tikz} and \ref{fig:both}

\end{document}

* Edit *

And the result of \listfiles is:

 *File List*
 article.cls    2007/10/19 v1.4h Standard LaTeX document class
  size10.clo    2007/10/19 v1.4h Standard LaTeX file (size option)
algorithm.sty    2009/08/24 v0.1 Document Style `algorithm' - floating environm
ent
   float.sty    2001/11/08 v1.3d Float enhancements (AL)
  ifthen.sty    2001/05/26 v1.1c Standard LaTeX ifthen package (DPC)
algpseudocode.sty    
algorithmicx.sty    2005/04/27 v1.2 Algorithmicx
 caption.sty    2010/01/09 v3.1m Customizing captions (AR)
caption3.sty    2010/01/14 v3.1m caption3 kernel (AR)
  keyval.sty    1999/03/16 v1.13 key=value parser (DPC)
subcaption.sty    2008/08/31 v1.0b Adding subcaptions (AR)
    tikz.sty    2010/10/13 v2.10 (rcs-revision 1.76)
     pgf.sty    2008/01/15 v2.10 (rcs-revision 1.12)
  pgfrcs.sty    2010/10/25 v2.10 (rcs-revision 1.24)
everyshi.sty    2001/05/15 v3.00 EveryShipout Package (MS)
  pgfrcs.code.tex
 pgfcore.sty    2010/04/11 v2.10 (rcs-revision 1.7)
graphicx.sty    1999/02/16 v1.0f Enhanced LaTeX Graphics (DPC,SPQR)
graphics.sty    2009/02/05 v1.0o Standard LaTeX Graphics (DPC,SPQR)
    trig.sty    1999/03/16 v1.09 sin cos tan (DPC)
graphics.cfg    2010/04/23 v1.9 graphics configuration of TeX Live
  pdftex.def    2011/05/27 v0.06d Graphics/color for pdfTeX
infwarerr.sty    2010/04/08 v1.3 Providing info/warning/message (HO)
 ltxcmds.sty    2011/04/18 v1.20 LaTeX kernel commands for general use (HO)
  pgfsys.sty    2010/06/30 v2.10 (rcs-revision 1.37)
  pgfsys.code.tex
pgfsyssoftpath.code.tex    2008/07/18  (rcs-revision 1.7)
pgfsysprotocol.code.tex    2006/10/16  (rcs-revision 1.4)
  xcolor.sty    2007/01/21 v2.11 LaTeX color extensions (UK)
   color.cfg    2007/01/18 v1.5 color configuration of teTeX/TeXLive
 pgfcore.code.tex
pgfcomp-version-0-65.sty    2007/07/03 v2.10 (rcs-revision 1.7)
pgfcomp-version-1-18.sty    2007/07/23 v2.10 (rcs-revision 1.1)
  pgffor.sty    2010/03/23 v2.10 (rcs-revision 1.18)
 pgfkeys.sty    
 pgfkeys.code.tex
  pgffor.code.tex
    tikz.code.tex
supp-pdf.mkii
pdftexcmds.sty    2011/04/22 v0.16 Utilities of pdfTeX for LuaTeX (HO)
ifluatex.sty    2010/03/01 v1.3 Provides the ifluatex switch (HO)
   ifpdf.sty    2011/01/30 v2.3 Provides the ifpdf switch (HO)
epstopdf-base.sty    2010/02/09 v2.5 Base part for package epstopdf
  grfext.sty    2010/08/19 v1.1 Managing graphics extensions (HO)
kvdefinekeys.sty    2011/04/07 v1.3 Defining keys (HO)
kvoptions.sty    2010/12/23 v3.10 Keyval support for LaTeX options (HO)
kvsetkeys.sty    2011/04/07 v1.13 Key value parser (HO)
etexcmds.sty    2011/02/16 v1.5 Prefix for e-TeX command names (HO)
epstopdf-sys.cfg    2010/07/13 v1.3 Configuration of (r)epstopdf for TeX Live

I'm using a mac. I have installed the latex packages using macports and those are the most recent versions available there.

share|improve this question
1  
Note that, even though \algorithmicindent sounds like a length modifiable via \setlength, it is actually a command. Therefore, you need to use \renewcommand{\algorithmicindent}{<len>}. That's just the way it is... –  Werner Jan 6 '12 at 20:00
    
@Werner: please, take a look at my comment on MarcoDaniel's answer. –  freitass Jan 6 '12 at 20:09
    
The algorithmic environment of algpseudocode (from the algorithmicx package) typesets its contents as a list which is flush with the left margin and a default item label width of 12pt. If you have a short algorithm (with fewer than 10 lines), it may seem like there's a gap between the item and the left margin. Is this the indent you're referring to that you want removed? –  Werner Jan 6 '12 at 20:24
    
@Werner: Maybe that is what I'm talking about. Please, check the example I have provided; I need the algorithm to be very close to the left side of the box. –  freitass Jan 6 '12 at 20:32

1 Answer 1

up vote 7 down vote accepted

Your provided example code has some mistakes. The example given in your previous question How to put an algorithm and a tikz drawing (objects of different type) side-by-side in the same float environment? had no mistakes.

I will start with a short analysis of your errors.

  1. First of all it seems that you haven't read any documentation. Sorry for the hard words.

  2. The argument of the environment subfigure is the width. You want to have two subfigure environments besides each other. A simple calculation shows that .5\linewidth+\quad+.5\linewidth is greater than \textwidth. (quad=\hspace{1em}). The result is an overfull hbox

  3. You set the following combination

    \fbox{
      \begin{environment}
    

    and this results in an extra horizontal space because you didn't comment the extra space. The correct form is

    \fbox{%
      \begin{environment}
    
  4. You can't encapsule subfigure within a \fbox. Inside subfigure you can use the environment adjustbox to frame the algorithmic environment with the correct width calculation. (Used in the example.)

  5. The algorithmic environment has no direct solution avoiding the indentation. In the example I redefined the environment itself by adding the length definition:

    \labelwidth0pt\labelsep0pt %%ADDED
    

    To the ifequal test. In this way you can use the optional argument of algorithmic as usual. (Thanks to Werner)

    A second approach (recommened) is to use the command patchmd provided by the package etoolbox. (For more information see the documentation). Normally the command patchcmd doesn't work with optional arguments. By a suggest trick (Thanks egreg) we can use the following combination:

    \expandafter\patchcmd\csname\string\algorithmic\endcsname%
      {\labelwidth 0.5em}{\labelwidth0pt\labelsep0pt}{}{}
    

    An explanation is given in the threat How can I use \patchcmd with commands with optional arguments?

Here is the example:

\documentclass{article}
\usepackage{algorithm}% http://ctan.org/pkg/algorithm
\usepackage{algpseudocode}% http://ctan.org/pkg/algorithmicx

\usepackage{caption}
\usepackage{subcaption}
\usepackage{tikz}
\usepackage{adjustbox}
\usepackage{etoolbox}

\makeatletter
\expandafter\patchcmd\csname\string\algorithmic\endcsname%
      {\labelwidth 0.5em}{\labelwidth0pt\labelsep0pt}{}{}
%\renewenvironment{algorithmic}[1][0]%
%   {%
%   \edef\ALG@numberfreq{#1}%
%   \def\@currentlabel{\theALG@line}%
%   %
%   \setcounter{ALG@line}{0}%
%   \setcounter{ALG@rem}{0}%
%   %
%   \let\\\algbreak%
%   %
%   \expandafter\edef\csname ALG@currentblock@\theALG@nested\endcsname{0}%
%   \expandafter\let\csname ALG@currentlifetime@\theALG@nested\endcsname\relax%
%   %
%   \begin{list}%
%      {\ALG@step}%
%      {%
%      \rightmargin\z@%
%      \itemsep\z@ \itemindent\z@ \listparindent2em%
%      \partopsep\z@ \parskip\z@ \parsep\z@%
%      \labelsep 0.5em \topsep 0.2em%\skip 1.2em 
%      \ifthenelse{\equal{#1}{0}}%
%         {\labelwidth0pt\labelsep0pt}% %%ADDED / CHANGED
%         {\labelwidth 1.2em}%
%      \leftmargin\labelwidth \addtolength{\leftmargin}{\labelsep}% Ok. the perfect leftmargin :-))
%      \ALG@tlm\z@%
%      }%
%   \setcounter{ALG@nested}{0}%
%   \ALG@beginalgorithmic%
%   }%
%   {% end{algorithmic}
%   % check if all blocks are closed
%   \ALG@closeloops%
%   \expandafter\ifnum\csname ALG@currentblock@\theALG@nested\endcsname=0\relax%
%   \else%
%      \PackageError{algorithmicx}{Some blocks are not closed!!!}{}%
%   \fi%
%   \ALG@endalgorithmic%
%   \end{list}%
%   }%
\begin{document}
\section{foo}
Text

\setlength\fboxsep{0pt}
\renewcommand\algorithmicindent{.5em}

\begin{figure}[!ht]

  \begin{subfigure}[b]{.5\linewidth}
  \begin{adjustbox}{minipage=\linewidth,frame}
    \begin{algorithmic}
      \Procedure{Euclid}{$a,b$}\Comment{The g.c.d. of a and b}
        \State $r\gets a\bmod b$
        \While{$r\not=0$}\Comment{We have the answer if r is 0}
          \State $a\gets b$
          \State $b\gets r$
          \State $r\gets a\bmod b$
        \EndWhile\label{euclidendwhile}
        \State \textbf{return} $b$\Comment{The gcd is b}
      \EndProcedure
    \end{algorithmic}
   \end{adjustbox}
    \caption{Algorithm}\label{fig:alg}
  \end{subfigure}%
  \begin{subfigure}[b]{.5\linewidth}
    \centering
    \tikz\draw [fill=red!20] (0,0) rectangle (3,2);
    \caption{tikz}\label{fig:tikz}
  \end{subfigure}
  \caption{Both}\label{fig:both}
\end{figure}

Text \ref{fig:alg} and \ref{fig:tikz} and \ref{fig:both}

\end{document}
share|improve this answer
    
That works for the indentation after the first line. Still, since I'm not using the algorithm environment, I need to get rid of the very first indent too. You can see what I am telling by putting the algorithm inside an \fbox with \setlength\fboxsep{0pt} –  freitass Jan 6 '12 at 20:07
    
Ok, sorry for that. There (at the question) is the working example... (seems familiar, right? ;) –  freitass Jan 6 '12 at 20:29
    
Sorry for taking so long to answer. I'll comment your statements separately: 1) True. Actually I am indeed the kind of guy who reads documentations, but currently I don't have much time to finish my document; 2) The purpose of the example was to show the space with the borders around the algorithm, I didn't look to the other things; 3) I didn't know about that. Thanks!; 4) Since I could get borders with that I thought it was ok; 5) I could not compile your example here, I am getting ...missing \item at the \Procedure... line. Tomorrow I will try the other options. –  freitass Jan 8 '12 at 0:56
    
Output of \listfiles added to the question... –  freitass Jan 8 '12 at 12:25
    
@freitass: You need really an update for all your packages. What system do you use? –  Marco Daniel Jan 8 '12 at 12:27

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.