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.

In trying to provide an answer to this question: Two column trouble - positioning of figures and text, I realized that I need to shift the wrapfigure up to account for the extra spacing that the \colorbox was adding. I adjusted the \intextsep length, which works fine, except at the top of a page. Note that the black box is not lined up with the top of the horizontal line, but the blue one is.

enter image description here

I found many hackish solutions which "fix" this problem (but add a small amount of blank space at the top of the page), including: \smash{}, \llap{}, \rlap{}, \hphantom{}, \vphantom{}, \ which all get TeX to know that we are not at the top of a page so that \intextsep gets applied.

Questions:

  1. What is the proper way to fix this spacing?

  2. How would I go about adding a zero height, zero width box at the top so that no space is added and that \intextsep gets applied, but makes TeX behave as if it is not at the top of page? Of course, an alternate solution to #1 may not require this, but am still interested in knowing.

Here is the code:

\documentclass{article}
\usepackage[demo]{graphicx}
\usepackage{wrapfig}
\usepackage{xcolor}

\setlength{\parindent}{0pt}
\setlength{\parskip}{\baselineskip}

\newcommand*{\WarningHead}[1]{%
  \colorbox{black}{\parbox{\linewidth}{\color{white}\textbf{#1}}}\vspace{10px}%
}

\newcommand*{\WrapFigure}[1]{%
  \setlength{\intextsep}{-3pt}%
  \begin{wrapfigure}{r}[0pt]{0.3\linewidth}%
    \raggedleft% right align the figures
    \includegraphics[width=0.95\linewidth]{#1}%
  \end{wrapfigure}
}%

\newcommand*{\lorem}{Lorem ipsum dolor sit amet, consectetur adipiscing elit. Duis pellentesque iaculis nunc eget congue. Etiam lobortis nisi velit. Proin tristique massa a lectus ullamcorper semper. Aenean ut dignissim diam. Integer ullamcorper eros nibh. Cras molestie neque quis lectus lobortis egestas. Maecenas vel tortor in nulla sagittis venenatis. Nulla ac eros dui, eget lacinia ante. Fusce elementum nisl ac tortor hendrerit id lacinia orci malesuada. Praesent eu iaculis mi. Vestibulum sodales tempor rutrum. }

\begin{document}\
\WrapFigure{foo}
\WarningHead{WARNING}
\lorem

\color{blue}
\WrapFigure{foo}
\WarningHead{WARNING}
\lorem
\end{document}
share|improve this question
    
Issuing a \vspace*{-2\baselineskip} before the start-of-a-new-page \Wrapfigure solves this. This takes care of the \parskip (set at \baselineskip) and a "blank entry" causing a \baselineskip. –  Werner Nov 8 '11 at 5:50
add comment

2 Answers

up vote 6 down vote accepted

try it this way:

\newcommand*\WarningHead[1]{%
  \colorbox{black}{\parbox{\linewidth}{\color{white}\textbf{#1}}}\vspace{10px}%
}

\newcommand*\WrapFigure[1]{\leavevmode%
  \setlength\intextsep{-3pt}%
  \begin{wrapfigure}{r}[0pt]{0.3\linewidth}%
    \raggedleft% right align the figures
    \includegraphics[width=0.95\linewidth]{#1}%
  \end{wrapfigure}
}%

enter image description here

to prevent the additional vertical space you can do

\newcommand*\WrapFigure[1]{%
  \ifhmode\else\leavevmode\vspace*{-\normalbaselineskip}\fi%
  \setlength\intextsep{-3pt}%
share|improve this answer
    
Shouldn't it be \dimexpr(-\normalbaselineskip-3pt)? –  Yiannis Lazarides Nov 8 '11 at 10:30
    
Correct, but I suppose that it already doesn't work in the expected way ... –  Herbert Nov 8 '11 at 10:38
    
Interestingly, this works great in this MWE, but adding \ifhmode\else\leavevmode\vspace*{-\normalbaselineskip}\fi just prior to \setlength{\intextsep} totally messes up the solution to two column trouble positioning of figures and text –  Peter Grill Nov 8 '11 at 19:00
    
are you using multicols or twocolumn option? –  Herbert Nov 8 '11 at 19:15
    
Nope. Using it exactly as in two column trouble positioning of figures and text, with the \ihfmode...\fi included. –  Peter Grill Nov 10 '11 at 4:50
add comment

Just insert an instruction such as \phantom{a} right after \begin{document}. I wouldn't call it "hackish" at all. :-)

share|improve this answer
    
But the problem will reappear if the wrapfigure starts a page somewhere else. –  Gonzalo Medina Nov 8 '11 at 2:10
    
That leaves extra space at the top of the page similar to hphantom{} and \vphantom{}. But you are right in that I did not specifically include that in my hackish solutions. –  Peter Grill Nov 8 '11 at 2:20
    
Inserting a \leavevmode at the start of the wrapped figure also works -- except that it also adds an extra amount of white space... –  Mico Nov 8 '11 at 11:59
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.