4

I have a multi-page table produced by long table package and I want to put each page of the longtable inside \shadowbox. Here is what I have tried:

\documentclass{article}
\usepackage{fancybox}
\usepackage{longtable}
\begin{document}
\shadowbox{\vbox{\begin{longtable}{|c|c|}\hline
Test&Test\\ \hline
Test&Test\\ \hline
Test&Test\\ \hline
Test&Test\\ \hline
..................... 
\end{longtable}}}
\end{document}

But then my table does not break across pages. How should I fix this?

Note that using mdframed is not an option for me. A solution should only use \shadowbox command from fancybox package. This is my assignment from LaTeX writing course that I do at my university.

EDit: The assignment is over. So if you can solve it, then post your solutions.

  • 1
    Using mdframed can be an option for you? – user11232 Apr 17 '12 at 5:50
  • 2
    Boxes cannot be broken across pages, and so creating graphical effects around them which can be broken is pretty tricky (hence the existence of mdframed). I am pretty suspicious of this being the real nature of an assignment: I think a lot of experienced LaTeX users would struggle to do this in the way you put it! – Joseph Wright Apr 17 '12 at 6:35
  • @HarishKumar: I think the page-break code in mdframed would clash with the one of longtable. So this wouldn't be an option as well. I wonder if tabu allows something like this. – Martin Scharrer Apr 17 '12 at 7:05
  • 2
    Your instructor has mislead you, the longtable chunks bear no resemblance the to visible units of the table that appear on each page. If you do want to do this you would have to insert the shadowbox in the longtable output routine as it re-assembles each page. – David Carlisle Apr 17 '12 at 8:38
  • 1
    Well if it's an assignment, code seems inappropriate? – David Carlisle Apr 17 '12 at 9:59
5

To use the command shadowbox you can use the package framed which can handle shadowbox:

\usepackage{fancybox}
\newenvironment{shadowframe}{%
  \def\FrameCommand{\fboxsep=\FrameSep \shadowbox}%
  \MakeFramed {\FrameRestore}}%
 {\endMakeFramed}
\usepackage[]{framed}

EDIT:

Although mdframed isn't a solution for you I want to provide an example with his package:

\documentclass{article}
\usepackage{xcolor}
\usepackage{longtable}
\usepackage[shadow=true,linewidth=2pt,everyline=true]{mdframed}
\begin{document}
\begin{mdframed}
\begin{longtable}{|c|c|}\hline
Test&Test\\ \hline
Test&Test\\ \hline
Test&Test\\ \hline
Test&Test\\ \hline
Test&Test\\ \hline
Test&Test\\ \hline
Test&Test\\ \hline
Test&Test\\ \hline
Test&Test\\ \hline
Test&Test\\ \hline
Test&Test\\ \hline
Test&Test\\ \hline
Test&Test\\ \hline
Test&Test\\ \hline
Test&Test\\ \hline
Test&Test\\ \hline
Test&Test\\ \hline
Test&Test\\ \hline
Test&Test\\ \hline
Test&Test\\ \hline
Test&Test\\ \hline
Test&Test\\ \hline
Test&Test\\ \hline
Test&Test\\ \hline
Test&Test\\ \hline
Test&Test\\ \hline
Test&Test\\ \hline
Test&Test\\ \hline
Test&Test\\ \hline
Test&Test\\ \hline
Test&Test\\ \hline
Test&Test\\ \hline
Test&Test\\ \hline
Test&Test\\ \hline
Test&Test\\ \hline
Test&Test\\ \hline
Test&Test\\ \hline
Test&Test\\ \hline
Test&Test\\ \hline
Test&Test\\ \hline
Test&Test\\ \hline
Test&Test\\ \hline
Test&Test\\ \hline
Test&Test\\ \hline
Test&Test\\ \hline
Test&Test\\ \hline
Test&Test\\ \hline
Test&Test\\ \hline
Test&Test\\ \hline
Test&Test\\ \hline
Test&Test\\ \hline
Test&Test\\ \hline
Test&Test\\ \hline
Test&Test\\ \hline
Test&Test\\ \hline
Test&Test\\ \hline
Test&Test\\ \hline
Test&Test\\ \hline
Test&Test\\ \hline
Test&Test\\ \hline
Test&Test\\ \hline
Test&Test\\ \hline
Test&Test\\ \hline
Test&Test\\ \hline
Test&Test\\ \hline
Test&Test\\ \hline
Test&Test\\ \hline
Test&Test\\ \hline
Test&Test\\ \hline
Test&Test\\ \hline
Test&Test\\ \hline
Test&Test\\ \hline
Test&Test\\ \hline
Test&Test\\ \hline
Test&Test\\ \hline
Test&Test\\ \hline
Test&Test\\ \hline
Test&Test\\ \hline
Test&Test\\ \hline
Test&Test\\ \hline
Test&Test\\ \hline
Test&Test\\ \hline
Test&Test\\ \hline
Test&Test\\ \hline
Test&Test\\ \hline
Test&Test\\ \hline
Test&Test\\ \hline
Test&Test\\ \hline
Test&Test\\ \hline
Test&Test\\ \hline
Test&Test\\ \hline
Test&Test\\ \hline
Test&Test\\ \hline
Test&Test\\ \hline
Test&Test\\ \hline
Test&Test\\ \hline
Test&Test\\ \hline
Test&Test\\ \hline
Test&Test\\ \hline
Test&Test\\ \hline
Test&Test\\ \hline
Test&Test\\ \hline
Test&Test\\ \hline
Test&Test\\ \hline
\end{longtable}
\end{mdframed}
\end{document}

enter image description here

  • This is certainly not a solution. I explicitly said in my question that any solution should only use \shadowbox from fancybox package so using framed package is not acceptable. – Simurgh12 Apr 18 '12 at 8:22
  • @user13617 The command shadowbox is used. Without the extra package you have to hack the package longtable. You can also write your own splitting algorithm. – Marco Daniel Apr 18 '12 at 12:04
  • Daneil: I knew before creating this post that I need to hack longtable. My point was that your answer does not help at all. – Simurgh12 Apr 19 '12 at 15:19
5
+100

You ought to modify the commands \LT@output and \LT@start from the package. The following works for the longtable version [2004/02/01 v4.11]. The biggest problem is that you cannot put \shadowbox directly around \box255 which stores the page contents. Second problem is that the shadowbox would go around the text before the table at its first page. I made the shadowbox a bit narrower (1.5% from both left and right) to get a better look on the last page.

\documentclass{article}

\usepackage{longtable} % version 
\makeatletter
\newlength{\LT@startpagetotal}
\def\LT@start{%
    \let\LT@start\endgraf
    \endgraf\penalty\z@\vskip\LTpre
    \dimen@\pagetotal
    \advance\dimen@ \ht\ifvoid\LT@firsthead\LT@head\else\LT@firsthead\fi
    \advance\dimen@ \dp\ifvoid\LT@firsthead\LT@head\else\LT@firsthead\fi
    \advance\dimen@ \ht\LT@foot
    \dimen@ii\vfuzz
    \vfuzz\maxdimen
    \setbox\tw@\copy\z@
    \setbox\tw@\vsplit\tw@ to \ht\@arstrutbox
    \setbox\tw@\vbox{\unvbox\tw@}%
    \vfuzz\dimen@ii
    \advance\dimen@ \ht
        \ifdim\ht\@arstrutbox>\ht\tw@\@arstrutbox\else\tw@\fi
    \advance\dimen@\dp
        \ifdim\dp\@arstrutbox>\dp\tw@\@arstrutbox\else\tw@\fi
    \advance\dimen@ -\pagegoal
    \ifdim \dimen@>\z@\vfil\break\fi
        \global\@colroom\@colht
    \ifvoid\LT@foot\else
    \advance\vsize-\ht\LT@foot
    \global\advance\@colroom-\ht\LT@foot
    \dimen@\pagegoal\advance\dimen@-\ht\LT@foot\pagegoal\dimen@
    \maxdepth\z@
    \fi
    \ifvoid\LT@firsthead\relax
      \LT@startpagetotal-\ht\LT@head\relax% MODIFIED %
      \copy\LT@head
    \else
      \LT@startpagetotal-\ht\LT@firsthead\relax% MODIFIED %
      \box\LT@firsthead
    \fi\nobreak
    \advance\LT@startpagetotal\pagetotal\relax% MODIFIED %
    \output{\LT@output}}
\def\LT@output{%
    \ifnum\outputpenalty <-\@Mi
    \ifnum\outputpenalty > -\LT@end@pen
        \LT@err{floats and marginpars not allowed in a longtable}\@ehc
    \else
        \setbox\z@\vbox{\unvbox\@cclv}%
        \ifdim \ht\LT@lastfoot>\ht\LT@foot
        \dimen@\pagegoal
        \advance\dimen@-\ht\LT@lastfoot
        \ifdim\dimen@<\ht\z@
            \setbox2\hbox to 0.97\textwidth{\box\@cclv\hss}%% MODIFIED %
            \ht2\dimexpr\ht2-\LT@startpagetotal\relax% MODIFIED %
            \setbox1\hbox{\shadowbox{\box2}}%% MODIFIED %
            \ht1\dimexpr\ht1+\LT@startpagetotal\relax% MODIFIED %
            \global\LT@startpagetotal0pt\relax% MODIFIED %
            \setbox\@cclv\vbox{\box1\copy\LT@foot\vss}%% MODIFIED %
            \@makecol
            \@outputpage
            \setbox\z@\vbox{\box\LT@head}%
        \fi
        \fi
        \global\@colroom\@colht
        \global\vsize\@colht
        \setbox2\hbox to 0.97\textwidth{\kern-0.015\textwidth\vbox% MODIFIED %
          {\unvbox\z@\box\ifvoid\LT@lastfoot\LT@foot\else\LT@lastfoot\fi}\hss}%% MODIFIED %
        \ht2\dimexpr\ht2-\LT@startpagetotal\relax% MODIFIED %
        \setbox1\hbox{\shadowbox{\box2}}%% MODIFIED %
        \ht1\dimexpr\ht1+\LT@startpagetotal\relax% MODIFIED %
        \global\LT@startpagetotal0pt\relax% MODIFIED %
        \noindent\box1\relax%
    \fi
    \else
    \setbox\@cclv\vbox{\unvbox\@cclv\copy\LT@foot}%% MODIFIED %
    \setbox2\hbox to 0.97\textwidth{\kern-0.015\textwidth\box\@cclv\hss}%% MODIFIED %
    \ht2\dimexpr\ht2-\LT@startpagetotal\relax% MODIFIED %
    \setbox1\hbox{\shadowbox{\box2}}%% MODIFIED %
    \ht1\dimexpr\ht1+\LT@startpagetotal\relax% MODIFIED %
    \global\LT@startpagetotal0pt\relax% MODIFIED %
    \setbox\@cclv\vbox{\box1\vss}%% MODIFIED %
    \@makecol
    \@outputpage
        \global\vsize\@colroom
    \copy\LT@head\nobreak
    \fi}
\makeatother

\usepackage{fancybox}
\usepackage{lipsum}
\begin{document}

\lipsum[1-7]

%\tracingmacros=1
%\tracingonline=1
\begin{longtable}{|c|c|}\hline
FIRST&HEAD\\\hline\endfirsthead
\hline RUNNING&HEAD\\\hline\endhead
RUNNING&FOOT\\\hline\endfoot
LAST&FOOT\\\hline\endlastfoot
Test&Test\\ \hline
Test&Test\\ \hline
Test&Test\\ \hline
Test&Test\\ \hline
Test&Test\\ \hline
\end{longtable}

\lipsum[8-11]

\begin{longtable}{|c|c|}\hline
FIRST&HEAD\\\hline\endfirsthead
\hline RUNNING&HEAD\\\hline\endhead
RUNNING&FOOT\\\hline\endfoot
LAST&FOOT\\\hline\endlastfoot
Test&Test\\ \hline
Test&Test\\ \hline
Test&Test\\ \hline
Test&Test\\ \hline
Test&Test\\ \hline
Test&Test\\ \hline
Test&Test\\ \hline
Test&Test\\ \hline
Test&Test\\ \hline
Test&Test\\ \hline
Test&Test\\ \hline
Test&Test\\ \hline
Test&Test\\ \hline
Test&Test\\ \hline
Test&Test\\ \hline
Test&Test\\ \hline
Test&Test\\ \hline
Test&Test\\ \hline
Test&Test\\ \hline
Test&Test\\ \hline
Test&Test\\ \hline
Test&Test\\ \hline
Test&Test\\ \hline
Test&Test\\ \hline
Test&Test\\ \hline
Test&Test\\ \hline
Test&Test\\ \hline
Test&Test\\ \hline
Test&Test\\ \hline
Test&Test\\ \hline
Test&Test\\ \hline
Test&Test\\ \hline
Test&Test\\ \hline
Test&Test\\ \hline
Test&Test\\ \hline
Test&Test\\ \hline
Test&Test\\ \hline
Test&Test\\ \hline
Test&Test\\ \hline
Test&Test\\ \hline
Test&Test\\ \hline
Test&Test\\ \hline
Test&Test\\ \hline
Test&Test\\ \hline
Test&Test\\ \hline
Test&Test\\ \hline
Test&Test\\ \hline
Test&Test\\ \hline
Test&Test\\ \hline
Test&Test\\ \hline
Test&Test\\ \hline
Test&Test\\ \hline
Test&Test\\ \hline
Test&Test\\ \hline
Test&Test\\ \hline
Test&Test\\ \hline
Test&Test\\ \hline
Test&Test\\ \hline
Test&Test\\ \hline
Test&Test\\ \hline
Test&Test\\ \hline
Test&Test\\ \hline
Test&Test\\ \hline
Test&Test\\ \hline
Test&Test\\ \hline
Test&Test\\ \hline
Test&Test\\ \hline
Test&Test\\ \hline
Test&Test\\ \hline
Test&Test\\ \hline
Test&Test\\ \hline
Test&Test\\ \hline
Test&Test\\ \hline
Test&Test\\ \hline
Test&Test\\ \hline
Test&Test\\ \hline
Test&Test\\ \hline
Test&Test\\ \hline
Test&Test\\ \hline
Test&Test\\ \hline
Test&Test\\ \hline
Test&Test\\ \hline
Test&Test\\ \hline
Test&Test\\ \hline
Test&Test\\ \hline
Test&Test\\ \hline
Test&Test\\ \hline
Test&Test\\ \hline
Test&Test\\ \hline
Test&Test\\ \hline
Test&Test\\ \hline
Test&Test\\ \hline
Test&Test\\ \hline
Test&Test\\ \hline
Test&Test\\ \hline
Test&Test\\ \hline
Test&Test\\ \hline
Test&Test\\ \hline
Test&Test\\ \hline
\end{longtable}

\lipsum[12-15]

\end{document}

How does the modification work:

We do two things: The macro \LT@output is the output routine for the longtable. This routine takes care of the page typesetting. Here we take the page contents (which is internally stored in \box255=\box\@cclv) and put \shadowbox around it, just not exactly, we have to fool around with \hbox and \vbox at different places to get the desired result. The problem is that for the first page of the longtable, this puts the text above the table into the box, which is frowned upon. Hence we modify \LT@start to measure the \pagetotal of the page when the table begins to be typeset. We do it after the table head is set so that we don't have to deal with the \depth of the previous contents. The length \LT@startpageottal is used to store this measure. Once we know how high is the text above the table, we make \shadowbox assume that the contents of the box are less high by this amount.

  • Thanks @percusse, I was missing a \noindent at one place. – yo' Apr 19 '12 at 21:40
  • This has only one problem. We only want table to be inside \shadowbox but this places both table and normal text inside \shadowbox. Is not there any way that we just put table inside \shadowbox and not the normal text? – Simurgh12 Apr 20 '12 at 1:49
  • I'll look into it. – yo' Apr 20 '12 at 5:38
  • The new version should work, I'm in a hurry now and will comment the code later. – yo' Apr 20 '12 at 6:42
  • You should comment the code @tohecz. :) – Alex Hirzel Jun 15 '12 at 23:49

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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