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.
First of all it seems that you haven't read any documentation. Sorry for the hard words.
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
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}
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.)
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}
I now am using the following workaround (i call it that way because it causes circumstances).
- ever algorithm has its own tex file but without any preamble and without includes etc.
- this way I cannot compile these files directly but need another tex using \input and all neccessary includes to compile this file
- this is cumbersome because I always need to change the tex for compiling when just changed a little on the pseudo-code
this way my code-tex is looking like this:
\begin{algorithm}
...
\end{algorithm
these "wrapper-tex" is looking like this:
\documentclass[12pt, letterpaper]{article}
\usepackage[utf8]{inputenc}
\usepackage{import}
\usepackage{amsmath}
\usepackage{amsfonts}
\usepackage[linesnumbered,ruled]{algorithm2e}
\begin{document}
\import{Algo}{MyAlgo.tex}
\end{document}
For not getting the line pitch of the surrounding document I use \renewcommand before unsing input on the code and after it again to setting it back.
When the code does not fit into the line latex display text, which actually occurs after the input, before the code itself (on the previous page if theres enough space left for text but not for code). I got it by adding a \newpage after the input of the code.
Using include does not show my pseudo code at all.
My include that way looks like:
\begin{flushleft}
some text
...
again some text
\renewcommand{\baselinestretch}{1.0}
\input{Algo/MyAlog.tex}
\renewcommand{\baselinestretch}{1.5}
\newpage
againagainsometext
...
u know
\end{flushleft}
...
I had also problems because in the tex with my texts I defined 12pt fontsize while I didn at first on my code-wrapper.
The pseudocode that way was enlarged after inputting it. Resizing it using recommendations of these
Reduce pseudocode font size (not global)
or othere did not help very much, I surrendered after over an hour trying so I reduced my pseudocode by dividing it into multiple/ separate algorithms and by deleting code commentary.
Best Answer
You can use
\Indp
(the reverse effect can be achieved with\Indm
):Perhaps you should consider defining some structure to avoid manually writing
\textbf{Begin}
,\textbf{End}
.