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.

Background

Using LyX to write a book. The source code listings shown in the book are graphics. The graphics should have a vertical rule beside them. But only graphics that are embedded within an Algorithm float. The code for the float resembles:

\begin{algorithm}[H]
\includegraphics{source/sql/query-formatted.sql.png}

\caption{\label{alg:Query-with-Formatting}Query with Formatting}

\end{algorithm}

There are scores of these example source code snippets, with more to come. Adding code to each one, manually, is undesirable.

Problem

Listing 1.1 in the following figure shows the placement character [H] nestled between the vertical rule and the graphic:

http://i.stack.imgur.com/B1i4k.png

The preamble used:

% Determine if the image is too wide for the page.
\makeatletter
\def\ScaleIfNeeded{%
  \ifdim\Gin@nat@width>\linewidth
    \linewidth
  \else
    \Gin@nat@width
  \fi
}
\makeatother

% Resize figures that are too wide for the page.
\let\oldincludegraphics\includegraphics
\renewcommand\includegraphics[2][]{%
  \oldincludegraphics[width=\ScaleIfNeeded]{#2}
  \graphicsalignment
}

% Do not centre graphics within Algorithm floats.    
\expandafter\def\expandafter\algorithm\expandafter{%
  \expandafter\let\expandafter\graphicsalignment\expandafter\relax
  \algorithm
  \vrule width 1em
}

\let\graphicsalignment\centering

Question

How do you eliminate the [H] argument so that the \vrule appears flush with the graphic, and placed "Here definitely"? (I have managed to do this in other ways, but then the graphics are no longer "Here definitely.")

Thank you!

share|improve this question
add comment

1 Answer

up vote 2 down vote accepted

I think what you want to do here is change

\expandafter\def\expandafter\algorithm\expandafter{%
  \expandafter\let\expandafter\graphicsalignment\expandafter\relax
  \algorithm
  \vrule width 1em
}

to

\let\oldalgorithm\algorithm
\let\endoldalgorithm\endalgorithm
\renewenvironment{algorithm}[1][htbp]{
        \let\graphicsalignment\relax
        \oldalgorithm[#1]
                \vrule width1em
                \kern1em
}{%
        \endoldalgorithm
}

This only works for graphics because TeX treats them as a single box. For a more normal usage of algorithm which contains an algorithmic environment, the coderule environment from the previous question would be more suitable.

share|improve this answer
    
Thank you. Almost works. The [H] parameter is gone, but the vertical space it imposed remains. (That is, there is space between the image and \vrule.) –  Dave Jarvis Dec 4 '10 at 23:23
    
Caused by width1em rather than width 1em? Or caused by \kern1em. I changed the first and removed the second. It works now, thank you. –  Dave Jarvis Dec 4 '10 at 23:26
    
@Dave: The space was from the \kern1em. –  TH. Dec 5 '10 at 0:20
    
Sadly, I had to shelve the idea of a LaTeX-based \vrule altogether, as it pushes the longer listings into the page margins. –  Dave Jarvis Dec 5 '10 at 0:23
    
@Dave: You could put the rule in the margin instead. –  TH. Dec 5 '10 at 0:34
add comment

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.