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}
\let\centering\relax
in the preamble will cancel \centering
everywhere, or to be more selective and only cancel in floats, you can try
\makeatletter
\let\old@float\@float
\def\@float{\let\centering\relax\old@float}
\makeatother
You will still need to edit the \floatsetup{}
line as lockstep described.
Best Answer
If you would like to center all tabular environments, I would recommend to create your own environment with a different name, which calls tabular. You could use that instead of redefining tabular itself.
But no problem, this can be done. Just use
\renewenvironment
, for example:This as addition to your first try redefining tabular.
You could redefine the floating table environment very similar:
You could also use etoolbox to patch the table environment or append
\centering
to the definition this way:or