# Override \centering to left justify an image

## Background

Dozens of source code snippets inserted into a document as images inside of Algorithm floats. Other images are screen captures, inserted as Graphics floats, to make a distinction. All Graphic floats should be centred, thus the preamble contains:

% Resize figures that are too wide for the page.
\let\oldincludegraphics\includegraphics
\renewcommand\includegraphics[2][]{%
\oldincludegraphics[width=\ScaleIfNeeded]{#2}
\centering
}

This works, as ScaleIfNeeded is defined elsewhere, resulting in all images being centred, including the Algorithm images.

## Problem

The Algorithm floats also contain images, and these images are centred, which is undesirable. The code for each Algorithm resembles:

\begin{algorithm}[H]
\includegraphics{source/sql/query-formatted.sql.png}
\caption{\label{alg:Query-with-Formatting}Query with Formatting}
\end{algorithm}

## Ideas

Tried:

% Centre algorithms.
\let\oldalgorithm\algorithm
\let\endoldalgorithm\endalgorithm
\renewenvironment{algorithm}[1][ht]{%
\begin{flushleft}
\oldalgorithm[#1]
\end{flushleft}
}%
{\endoldalgorithm}

This results in a number of errors ("Missing } inserted", amongst others).

## Question

How can images embedded exclusively within Algorithm floats be left-justified?

## Bonus Question

How can a thick, left-hand vertical rule (or any stylistic change) be made to the Algorithm floats? For example, the left-hand border on the "batch source text" from this indirectly-related question. (Or any source code snippets on SuperUser.com, for that matter.)

Thank you!

-

This is untested, but how about something like the following?

\expandafter\def\expandafter\algorithm\expandafter{%
\expandafter\let\expandafter\centering\expandafter\relax
\algorithm
}

This makes \centering do nothing in the algorithm environment. (There's actually a better way to do such things with the etoolbox package.) If you want, you could replace \relax with \raggedright (or better \RaggedRight from the ragged2e package) if you really want that behavior. The flushleft environment is not what you want since that's going to add extra space.

Edit:
Actually, I've got a better idea than redefining \centering locally which just feels wrong. How about this:

\let\oldincludegraphics\includegraphics[2][]{%
\oldincludegraphics[width=\ScaleIfNeeded]{#2}%
\graphicsalignment
}
\expandafter\def\expandafter\algorithm\expandafter{%
\expandafter\let\expandafter\graphicsalignment\expandafter\relax
\algorithm
}
\let\graphicsalignment\centering

Bonus Question:
Here's one idea for getting a rule.

\newdimen\coderulewidth \coderulewidth=1em
\newdimen\coderulesep \coderulesep=1em
\newdimen\coderuleseptemp
\newbox\codebox
\newenvironment{coderule}[1][\coderulesep]{%
\par
\coderuleseptemp=#1\relax
\setbox\codebox=\vbox\bgroup
\linewidth=\dimexpr\linewidth-\coderulewidth-\coderulesep\relax
\hsize=\linewidth
}{%
\egroup
\noindent
\strut
\hbox{%
\vrule width\coderulewidth
\kern\coderuleseptemp
\box\codebox
}%
}

As an example of usage, here are a few grafs with the thick rule on the left (this example needs the lipsum package).

\begin{coderule}
\lipsum[1-2]
\end{coderule}

For an algorithmic, the algorithm is already indented, so you can use the optional argument (or just redefine \coderulesep).

\begin{algorithm}
\begin{coderule}[0pt]
\begin{algorithmic}
\STATE $x\gets0$
\STATE $y\gets1$
\STATE $z\gets x+y$
\end{algorithmic}
\end{coderule}
\end{algorithm}
-
@TH.: The revised version is brilliant. Am learning a lot from you LaTeX wizards. Thank you for the bonus answer, too. – Dave Jarvis Dec 4 '10 at 19:06
@TH.: For the bonus question, I should have mentioned that I'm using LyX, so adding code to each Algorithm float manually is not an option. Am going to try and make the coderulesep automatically apply. Thanks! – Dave Jarvis Dec 4 '10 at 19:14
@TH.: Almost have the vertical rule working by coding \vrule width 1em to the \expandafter section. The \vrule is split from the image with [H]. – Dave Jarvis Dec 4 '10 at 19:48
@Dave: One idea would be to save \algorithm and \endalgorithm as before and then use \renewenvironment to capture the contents of the algorithm in a \vbox as I did in coderule. Then, after the \egroup which ends the \vbox, use the saved \algorithm and \endalgorithm along with the rule and the box. (I don't use LyX though so I'm not really sure what you can and cannot do.) – TH. Dec 4 '10 at 21:42
@Dave: Oh, sorry. I forgot context. I deleted that other comment. – TH. Dec 4 '10 at 22:33