# Adding a footnote with vwcol

I have a document such as this, where I use a vwcol. I also would like to add a footnote within the text.

\documentclass{article}

\usepackage{vwcol}

\begin{document}
\begin{vwcol}[widths={.5,.5}, sep=.8cm, justify=flush]

Lorem ipsum dolor sit amet, consectetur adipiscing elit. Quisque mauris purus, rhoncus in scelerisque vitae, viverra ac quam. Pellentesque habitant morbi tristique senectus\footnote{et netus et malesuada fames ac turpis egestas}. Duis in ligula sed massa aliquam accumsan. Aliquam erat volutpat. Donec nec lectus sit amet turpis pellentesque posuere vel a lorem. Pellentesque laoreet nisl eu justo varius, quis pharetra ligula iaculis.
\end{vwcol}
\end{document}


However, the footnote doesn't show up. The footnote number within the paragraph is shown, but the number is wrong (3 rather than 1 in this example).

I would like to have a footnote at the bottom of the page (the same way it appears when you comment out the lines that set the vwcol environment). How can I get footnotes to show when using a vwcol?

There are two issues in this situation. The first issue is that vwcol sets the contents in a \parbox, which does not display footnotes. See Why does \parbox lose footnotes? for more discussion, and also a solution by redefining \@footnotetext.

The second issue is that vwcol processes the same input several times in a loop, to compute the correct distribution of the columns. Every time the input is processed the footnote counters are increased. The final run is actually used in the document, with the shown footnote counter having skipped the values of the previous runs.

To address this issue you can process the footnotes only at the first run (to show the correct numbers at the bottom of the page and to prevent the same footnote being added multiple times). However, now the numbers are not shown in the paragraph, because that is the result of the last run. Therefore you can print only the number using \footnotemark, and reset this number before every run to the initial value.

A relatively easy way to accomplish the above is to define a new footnote command for use within vwcol environments (\footnotevwc in the MWE below). The necessary modifications to the rest of the code are minor (8 lines of code in total) but because vwcol uses \NewEnvironment from the environ package unfortunately the code cannot be patched easily. Instead a full redefinition of the environment is used in the code below, copied from the source of vwcol. The code for the footnotes in a \parbox from the other question is another 20 lines.

Full MWE:

\documentclass{article}
\usepackage[totalheight=8cm]{geometry}
\usepackage{vwcol}
\makeatletter
\RenewEnviron{vwcol}[1][]{%
\par\noindent
\vwcolsetup{#1}%
\splittopskip=\ht\strutbox
\expandafter\vwcol@process@widths\expandafter{\vwcol@widths}%
\vwcol@para@setup
\ifdim\vwcol@sep=1sp
\vwcol@totalwidth=\linewidth
\else
\vwcol@totalwidth=\numexpr
\vwcol@totalwidth+(\vwcol@Ncols-1)*\vwcol@sep
\relax sp
\if@vwcol@presep
\fi
\if@vwcol@postsep
\fi
\fi
\ifdim\vwcol@totalwidth > \linewidth
\vwcol@PackageWarning{%
Total width of columns plus their separations
is greater than the linewidth^^J\space\space
(by \the\vwcol@totalwidth\space - \the\linewidth\space =
\the\dimexpr \vwcol@totalwidth-\linewidth\relax)}%
\fi
\ifnum\vwcol@Nlines=0%
\gdef\footnotereset{exec}%%%% set flag to process footnotes at first run
\setcounter{fntemp}{\value{footnote}}%%%% store initial footnote counter
\setbox\vwcol@plainbox\hbox{%
\parbox{\vwcol@averagewidth}{\vwcol@justify\BODY}}%
\gdef\footnotereset{reset}%%%% don't process footnotes after first run
\vwcol@Nlines=\numexpr
(\ht\vwcol@plainbox+\dp\vwcol@plainbox)/
(\baselineskip*\vwcol@Ncols)
\relax
\@tempcnta=1%
\setcounter{footnote}{\value{fntemp}}%%%% reset counter for \footnotemark
\savebox\vwcol@outputbox{%
\hbox to \vwcol@totalwidth{\vwcol@{\BODY}}}%
\ifnum\@tempcnta>\vwcol@maxrecursion
\vwcol@PackageError{%
The estimated number of lines is greater than
\the\vwcol@maxrecursion\space lines too small,%
^^J\space\space
so I gave up (last tried maximum value of
[lines=\the\vwcol@Nlines])%
}{%
Text will be truncated in the multicolumns;
^^J\space\space
number of lines explicitly or increase
[maxrecursion=\the\vwcol@maxrecursion].%
}%
\fi
\fi
\repeat
\usebox\vwcol@outputbox
\else
\hbox to \vwcol@totalwidth{\vwcol@{\BODY}}%
\vwcol@PackageError{%
Not enough lines to fit the entire text;
some text has been truncated.^^J\space\space
Increase [lines=\the\vwcol@Nlines] to fit more%
}{%
Or remove [lines=\the\vwcol@Nlines] altogether
to have 'vwcol' estimate the value.}%
\fi
\fi\par}

\def\fnexec{exec} % comparison value for \ifx
\newcounter{fntemp} % counter to store initial footnote number
\newcommand{\footnotevwc}[1]{%
% process footnotes if flag is set to 'exec', i.e., first run
% otherwise print only the mark (using the initial number)
\ifx\footnotereset\fnexec\footnote{#1}\else\footnotemark\fi%
}

% macros for showing footnotes outside of \parbox
\newcommand{\global@insert}[2]% #1=box number, #2=vertical list
{\bgroup
\setbox\@tempboxa=\box#1
\global\setbox#1=\vbox{\unvbox\@tempboxa #2}
\egroup}

\long\def\@footnotetext#1{\global@insert\footins{%
\reset@font\footnotesize
\interlinepenalty\interfootnotelinepenalty
\splittopskip\footnotesep
\splitmaxdepth \dp\strutbox \floatingpenalty \@MM
\hsize\columnwidth \@parboxrestore
\protected@edef\@currentlabel{%
\csname p@footnote\endcsname\@thefnmark
}%
\color@begingroup
\@makefntext{%
\rule\z@\footnotesep\ignorespaces#1\@finalstrut\strutbox}%
\color@endgroup}}%
\makeatother

\begin{document}
Footnote\footnote{before}
\begin{vwcol}[widths={.5,.5}, sep=.8cm, justify=flush]
Lorem ipsum dolor sit amet, consectetur adipiscing elit.
Quisque mauris purus, rhoncus in scelerisque vitae, viverra ac quam.
Pellentesque habitant morbi tristique
senectus\footnotevwc{et netus et malesuada fames ac turpis egestas}.
Duis in ligula sed massa aliquam accumsan.\footnotevwc{Aliquam erat volutpat.}
Donec nec lectus sit amet turpis pellentesque posuere vel a lorem.
Pellentesque laoreet nisl eu justo varius, quis pharetra ligula iaculis.
\end{vwcol}
Footnote\footnote{after}
\end{document}


Result (note that the geometry package is used to make the pages smaller):