[Tex/LaTex] Override \centering to left justify an image

algorithmsgraphicshorizontal alignment


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.

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


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

\caption{\label{alg:Query-with-Formatting}Query with Formatting}



% Centre algorithms.

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


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!

Best Answer

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


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.

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


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

\newdimen\coderulewidth \coderulewidth=1em
\newdimen\coderulesep \coderulesep=1em
                \vrule width\coderulewidth

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


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

\STATE $x\gets0$
\STATE $y\gets1$
\STATE $z\gets x+y$