Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

How can I center a codebox (from clrscode3e package) horizontally in a document ?

share|improve this question
1  
The width of the codebox is variable. Thus strictly "centering" is not possible. You could only increase the left margin to move it to the right. –  Leo Liu Dec 1 '11 at 6:33

1 Answer 1

up vote 5 down vote accepted

Since the codebox is ultimately typeset in a tabbing environment, its width may vary. Moreover, it is typeset in a box which is only processed at the end of the codebox environment. As such, you would need a slight work-around in order to center it horizontally.

The varwidth package is capable of shrinking its width to the natural width of its contents. As such, wrapping the codebox environment within a varwidth environment allows for centering capability:

enter image description here

\documentclass{article}
\usepackage[showframe]{geometry}% http://ctan.org/pkg/geometry
\usepackage{clrscode3e}% http://www.cs.dartmouth.edu/~thc/clrscode/
\usepackage{varwidth}% http://ctan.org/pkg/varwidth
\begin{document}
\begin{center}
  \begin{varwidth}{\linewidth}
    \begin{codebox}
      \Procname{$\proc{Insertion-Sort}(A)$}
      \li \For $j \gets 2$ \To $\attrib{A}{length}$
      \li \Do $\id{key} \gets A[j]$
      \li \Comment Insert $A[j]$ into the sorted sequence $A[1 \twodots j-1]$.
      \li $i \gets j-1$
      \li \While $i > 0$ and $A[i] > \id{key}$
      \li \Do
            $A[i+1] \gets A[i]$
      \li $i \gets i-1$
          \End
      \li $A[i+1] \gets \id{key}$
      \End
    \end{codebox}
  \end{varwidth}
\end{center}
\end{document}

The varwidth environment is set to the maximum (\linewidth), since it will shrink to the desired width if the codebox is not as wide.

The showframe option to geometry allows for a visual of the text block boundaries.


My original approach was to use the savepos module of the zref package to mark the left-most and right-most points in the code via the macro \zsavepos{<label>}. Then, using \zposx{<ref>}, one can extract the x-coordinate (in small point sp units) of the label. So, marking the left-most point using \zsavepos{codeL} and the right-most point using \zsavepos{codeR},

\dimexpr\zposx{codeR}sp-\zposx{codeL}sp\relax

gives the horizontal width (or dimension) of the codebox. Subsequently, putting the entire codebox environment in a minipage of this width allows us to constrain the horizontal span and center the object. The correct width also avoids overfull \hbox warnings (if a chosen minipage width is too small) or incorrect centering (if a chosen minipage width is too large).

\documentclass{article}
\usepackage[showframe]{geometry}% http://ctan.org/pkg/geometry
\usepackage{clrscode3e}% http://www.cs.dartmouth.edu/~thc/clrscode/
\usepackage[savepos]{zref}% http://ctan.org/pkg/zref
\begin{document}
\begin{center}
  \begin{minipage}{\dimexpr\zposx{codeR}sp-\zposx{codeL}sp\relax}
    \begin{codebox}
      \Procname{\zsavepos{codeL}$\proc{Insertion-Sort}(A)$}
      \li \For $j \gets 2$ \To $\attrib{A}{length}$
      \li \Do $\id{key} \gets A[j]$
      \li \Comment Insert $A[j]$ into the sorted sequence $A[1 \twodots j-1]$.\zsavepos{codeR}
      \li $i \gets j-1$
      \li \While $i > 0$ and $A[i] > \id{key}$
      \li \Do
            $A[i+1] \gets A[i]$
      \li $i \gets i-1$
          \End
      \li $A[i+1] \gets \id{key}$
      \End
    \end{codebox}
  \end{minipage}
\end{center}
\end{document}

The above code (or procedure) was taken directly from the clrscode3e package documentation. The right-most label depends on the code segment being typeset, while the left-most label is typeset to the left of the procedure name. However, it is not necessary for the final production and centering capabilities. Since this uses the zref package, at least two compiles are required to obtain the correct positioning of the labels/references.

share|improve this answer
    
zref is overkill. It is possible to use varwidth environment instead of minipage to write codebox. Then it can be centered as a wide box. However, I still think it is better to indent but not center it. –  Leo Liu Dec 1 '11 at 7:01
    
@LeoLiu: I'm following the OP's request for centering, but why would zref be overkill? varwidth is only capable of shrinking to some natural width, which in this case is set by tabbing for indenting purposes. In fact, tabbing is set to be as wide as "two digits plus 9 \textbf{else}'s". –  Werner Dec 1 '11 at 7:12
    
@LeoLiu: For some strange reason my original attempts with varwidth didn't work. However, you were right, and I've added it as a shorter alternative to centering the codebox. –  Werner Dec 1 '11 at 7:53

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.