TransWikia.com

tufte-latex: Full-width algorithm2e blocks

TeX - LaTeX Asked by aaronsnoswell on March 16, 2021

I’m using documentclass{tufte-book} and trying to get a full-width algorithm environment (specifically, using algorithm2e).

Minimum working example:

documentclass{tufte-book}

title{Full-width algorithm2e algorithms}

usepackage{lipsum} % for dummy text only

usepackage[lined, ruled]{algorithm2e}

begin{document}

lipsum[2]

begin{algorithm}[H]
    tcp{Main loop}
    While{not converged}{
        $a = a + 1$ ;
    }
    caption{A text-width algorithm}
end{algorithm}

lipsum[2]

marginnote[10pt]{This algorithm should be full-width, but isn't}
begin{fullwidth}
begin{algorithm}[H]
    tcp{Main loop}
    While{not converged}{
        $a = a + 1$ ;
    }
    caption{A full-width algorithm}
end{algorithm}
end{fullwidth}

lipsum[2]

end{document}

Which renders:

Screenshot of rendered output from minimum working example

In the algorithm2e.sty source-code, I found the dimension algowidth defined about half way through the document.

%[snip]
newdimenalgowidth%
%[snip]

So I’ve tried adjusting this length using setlength{algowidth}{XXX} in my example, but it doesn’t seem to have any effect.

  • How can I achieve full-width algorithm2e algorithm blocks with tufte-latex?
  • Bonus marks if algorithm* can be adjusted to be full-width by default.
  • Extra bonus marks if the algorithm caption can also be moved to hang in the margin, as is the style with tufte-latex full width figures and tables.

Possibly related questions that don’t seem to answer this:

2 Answers

This might not be the most ideal solution since you loose the floating behaviour of the algorithmby enclosing it in a minipage, but you nevertheless already deactivated the float mechanism by adding [H]...

enter image description here

documentclass{tufte-book}

title{Full-width algorithm2e algorithms}

usepackage{lipsum} % for dummy text only

usepackage[lined, ruled]{algorithm2e}

newenvironment{widealgorithm}{begin{fullwidth}
begin{minipage}{linewidth}
begin{algorithm}[H]}{end{algorithm}
end{minipage}
end{fullwidth}}

begin{document}

lipsum[2]

begin{algorithm}[H]
    tcp{Main loop}
    While{not converged}{
        $a = a + 1$ ;
    }
    caption{A text-width algorithm}
end{algorithm}

lipsum[2]


begin{fullwidth}
begin{minipage}{linewidth}
begin{algorithm}[H]
    tcp{Main loop}
    While{not converged}{
        $a = a + 1$ ;
    }
    caption{A full-width algorithm}
end{algorithm}
end{minipage}
end{fullwidth}

lipsum[2]

begin{widealgorithm}
    tcp{Main loop}
    While{not converged}{
        $a = a + 1$ ;
    }
    caption{A full-width algorithm}
end{widealgorithm}


end{document}

Correct answer by leandriis on March 16, 2021

To solve this issue also for floating algorithms (I had one that was also a whole page) you can define a new float and put the minipage into that float. This is a variation of this answer. If I interpret this correctly, this should require the float package, which tufte-latex loads for you.

% Reusing the preamble of leandriis' excellent answer
documentclass{tufte-book}
title{Floaty Full-width algorithm2e algorithms}
usepackage{lipsum} % for dummy text only
usepackage[lined, ruled]{algorithm2e}

% Define a new float with name myalgo
% Default placement tbp and a file extension mya (for temp files)
% Cf. https://ctan.mc1.root.project-creative.net/macros/latex/contrib/float/float.pdf
newfloat{myalgo}{tbp}{mya}

% create a new environment called floatyalgorithm with one parameter (our algorithm code)
% Using this creates a new float through the myalgo float we defined above,
% which is filled with a minipage of full page width.
% The width is defined as the sum of a line, the margin and the sapce in between (since the fullwidth environment cannot help us here).
% For the variables names cf. https://www.overleaf.com/learn/latex/page_size_and_margins
% As in leandriis' answer, use [H] for the algorithm to fill the space.
newenvironment{floatyalgorithm}[1][tbp]%
{begin{myalgo}[#1]
begin{minipage}{linewidth+marginparsep+marginparwidth}
begin{algorithm}[H]}%
{end{algorithm}
end{minipage}
end{myalgo}
}


begin{document}

begin{floatyalgorithm}[tbp]
  $m := m'$;
  caption{lipsum[2]}
end{floatyalgorithm}    

end{document}

I have not extensively tested this. For my use case it worked nicely, but there might be some unwanted interactions with full pages and the content of the margin.

Answered by m00am on March 16, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP