[Tex/LaTex] Lettrine and Wrapfig

lettrinewrapfigure

How can I use lettrine at the start of a paragraph used in the wrapfigure environment.

Consider the following code:

\documentclass{article}

\RequirePackage[demo]{graphicx}
\usepackage{lettrine}
\usepackage{wrapfig}
\newcounter{cnt}\setcounter{cnt}{0}
\def\t{\stepcounter{cnt}\thecnt. cat sat on the mat. }



\begin{document}
    \begin{wrapfigure}{r}{0.3\textwidth}
        \vspace{1ex}
        \includegraphics[width=\linewidth,height=1in]{image}
    \end{wrapfigure}
    \lettrine{T}{he cat sat} \t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t
\end{document}

Which yields the following result, I have shifted the image down so that the problem is made more apparent, ie, wrapfig is ignored.

Image

NOTE: David Carlisle has provided a pretty good answer so far, however, the following anomalies occur with the output, specifically, an update was made to David's original code to allow the modified lettrine (wflettrine{...}) to take the optional arguments in order to control the letter height etc, however, by default, the following output is returned if the optional argument lines=5 is now parsed.

wflettrine5

The above was achieved with the code command:

\wflettrine[lines=5]{T}{he cat sat}

And it is clear to see under the red-highlighted region, that the drop-cap letter is not taking up the whole space, strangely enough, the same behavior was observed on my machine with the default computer-modern font, under a normal use of lettrine.

By changing the font to Helvetica, via the use of the following in the preamble:

\usepackage{helvet}
\renewcommand{\familydefault}{\sfdefault}

And executing the same command, yields the following output.

wflettrine_helvet

And it is clear that the previous problem is resolved (by the correct drop-cap usage under the green highlighted section), however, this has pushed the wrapfig component to the right, as can be seen under the red-highlighted section.

It appears as though the wrapfig has been pushed to the right by what appears to be the same amount as the width of the dropcap letter 'T'.

Best Answer

enter image description here

\documentclass{article}

\RequirePackage[demo]{graphicx}
\usepackage{helvet}\renewcommand{\familydefault}{\sfdefault}
\usepackage{lettrine}
\usepackage{wrapfig}
\newcounter{cnt}\setcounter{cnt}{0}
\def\t{\stepcounter{cnt}\thecnt. cat sat on the mat. }


\newdimen\tttaa
\newdimen\tttbb

\renewcommand\thepage{\the\numexpr(\value{page}+1)/2\relax}

\makeatletter
\def\merge@ps{\afterassignment\merge@ps@\tttbb}

\def\merge@ps@{\afterassignment\merge@ps@@\tttaa}


\def\merge@ps@@{%
\afterassignment\reset@WF@ps\dimen@\WF@ps\valign
%\showthe\count@
\ifnum\count@>\@ne
\advance\count@\m@ne
\expandafter\merge@ps
\fi
}


\def\reset@WF@ps{\afterassignment\reset@WF@ps@\dimen@ii}

\def\reset@WF@ps@#1\valign{%
\edef\new@wf@ps{\new@wf@ps
  \the\dimexpr\dimen@+\tttbb\relax\space
  \the\dimexpr\dimen@ii-\tttbb\relax\space}%
 \def\WF@ps{#1}}


\newcommand\wflettrine[3][]{%
  \setbox\tw@\hbox{\lettrine[#1]{#2}{#3}\global\let\gtmp\L@parshape}%
  \afterassignment\wf@getoffset\count@\gtmp\hoffset
  \setbox\WF@box\hbox{\kern-\dimen@\box\WF@box\kern\dimen@}%
  \noindent\box\tw@
    \def\new@wf@ps{}%
    \afterassignment\merge@ps\count@\gtmp
    \edef\WF@ps{\new@wf@ps\space\WF@ps}%
    \@@parshape\c@WF@wrappedlines\WF@ps\z@\columnwidth}


\def\wf@getoffset{\afterassignment\wf@get@ffset\dimen@}
\def\wf@get@ffset#1\hoffset{}

\makeatother

\begin{document}
%%%%%%%%%%%%%%

    \begin{wrapfigure}{r}{0.3\textwidth}
        \vspace{1ex}
        \includegraphics[width=\linewidth,height=1in]{image}
    \end{wrapfigure}
    \wflettrine[lines=5]{T}{he cat sat}
    \t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t
    \t\t\t\t\t\t\t\t\t


%%%%%%%%%%%%%%

    \begin{wrapfigure}{r}{0.3\textwidth}
        \vspace{1ex}
        \includegraphics[width=\linewidth,height=1in]{image}
    \end{wrapfigure}
    \wflettrine{T}{he cat sat}
    \t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t
    \t\t\t\t\t\t\t\t\t


%%%%%%%%%%%%%%%%%%%%%%%
    \lettrine[lines=5]{T}{he cat sat}
    \t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t
    \t\t\t\t\t\t\t\t\t


%%%%%%%%%%%%%%%%%%%%%%%

    \begin{wrapfigure}{r}{0.3\textwidth}
        \vspace{1ex}
        \includegraphics[width=\linewidth,height=1in]{image}
    \end{wrapfigure}
    The cat sat
    \t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t
    \t\t\t\t\t\t\t\t\t


\end{document}
Related Question