[Tex/LaTex] Available width inside algorithm2e

algorithm2ewidth

I wanted to use an environment inside an algorithm2e and it turned out, that it is too wide. I tried to draw some boxes to see the width, and it turned out that \textwidth is too large inside the algorithm. The framed environment however adheres to the correct size.

My question is: How can I access the available width in the algorithm area?

An example using a <code>frames</code> environment, which works fine and a box of width <code>\textwidth</code>, which is too large.

Here is a MWE:

\documentclass[a4paper,draft]{article}
\usepackage[ruled]{algorithm2e}
\usepackage{framed} 

\begin{document}
  \begin{algorithm}[h]
    \begin{framed}
      something in a framed environment
    \end{framed}
    \fbox{\parbox{\textwidth}{
      something with textwidth
    }}
    \caption{Some algorithm.}
  \end{algorithm}
\end{document} 

Best Answer

The algorithm environment increases left and right margins of its contents, so you have to substract them from textwidth:

\documentclass[a4paper,draft]{article}
\usepackage[utf8]{inputenc}
\usepackage[ruled]{algorithm2e}

\usepackage{framed}

\begin{document}

  \begin{algorithm}[h]
    \begin{framed}
      something in a framed environment
    \end{framed}
Some text… Some text… Some text… Some text… Some text… Some text… Some text… Some text…Some text… Some text… Some text… Some text… \\[1ex]
    \fbox{\parbox{\dimexpr\textwidth-2\algomargin\relax}{something with textwidth }}\\[1ex]

    \caption{Some algorithm.}
  \end{algorithm}
\end{document} 

enter image description here

Related Question