# Long table (longtabu) and font style issues

I use the longtabu environment for my tables to span it across several pages. This works great, however, I always change the font size and type as you will see in my example. This worked also very well until I tried to combine long tables and the new font style. I always put the font definitions within the table but outside the tabu environment as latex always throws an error when it is inside the tabu environment. So, I guess, you already see my problem. The longtabu environment does not work within a table environment but I cannot put my font definitions within the longtabu environment. Is there a way around it? Can I create a box or so around the longtabu? The problem is when I do not put the font definitions within the table environment, all the text that comes after that table gets the same font. Here's my code:

\documentclass[10pt,DIV=12,a4paper,numbers=noenddot]{scrreprt}

\usepackage[english]{babel}
\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}

\usepackage{tabu}
\usepackage{multicol}
\usepackage{multirow}
\usepackage{longtable}
\usepackage{booktabs}

\usepackage{tabularx}
\usepackage{anyfontsize}

\newcommand{\changefont}[3]{\fontfamily{#1} \fontseries{#2} \fontshape{#3} \selectfont}

\begin{document}

\begin{table}

\changefont{phv}{m}{n} % Change font to Helvetica

\fontsize{6}{9} \selectfont{ % Change font size and line spacing to 6 and 9 respectively

\begin{longtabu}{ll}

Longtable & Longtable \\
Longtable & Longtable \\
Longtable & Longtable \\
Longtable & Longtable \\
Longtable & Longtable \\

\end{longtabu}

}

\caption{This is a caption}

\end{table}

%% New example code
{

\changefont{phv}{m}{n}

\fontsize{6}{9} \selectfont

\begin{longtabu}{ll}

\toprule

\rowfont{\bfseries\itshape}

Longtable & Longtable \\
Longtable & Longtable \\
Longtable & Longtable \\
Longtable & Longtable \\
Longtable & Longtable \\

\bottomrule

\end{longtabu}

}

This is text after the long table and shoud be in standard latex format again. Apparently, it is not as you can see from
the baseline stretch which seems not to be 1.2. However, the font type seems to be ok.

\end{document}

-
It should work if you leave out the table environment and just move the { which is after \selectfont before \changefont. – Stephan Lehmke Feb 6 '13 at 12:24
Right, I just swapped the two lines \changefont and \fontsize and indeed the font after the table is then times new roman again. However, the font size is still set to 6 and 9. What I could do is to reapply the \fontsize command with \selection{} after the table to set it back to normal font size. The problem is that the two parameters for font size and line spacing seem not to correspond with the standard latex font. so \fontsize{10}{1.2} does not restore to the original font type. In the preamble I specified: \renewcommand{\baselinestretch}{1.2}. So I don't know how to set the paramters... – Josh Feb 6 '13 at 12:38
Swapping the lines is not enough, the { is in the wrong place. You need {\changefont{phv}{m}{n}\fontsize{6}{9} \selectfont. \selectfont is just a switching command, it doesn't take an argument. – Stephan Lehmke Feb 6 '13 at 12:42
Thanks for your comment stephan. I added another example above as I understood it from your explanation. However, it still doesn't quite work. The baselinestretch is still not correct. The font type itself seems to be ok but it's just a guess too :) Sorry, but to get this working is very important for me. I spent so much time on it already and I need to continue with the actual writing as soon as possible... – Josh Feb 6 '13 at 13:14
The example is ok. Are you sure that you didn't compile a different document where a spurious \fontsize{6}{9} were still active at the end? – Ulrike Fischer Feb 6 '13 at 13:46

A longtabu in a table environment doesn't make sense, because table cannot be split across pages. It's not very clear what you want to achieve. However, defining a new environment could be the solution.

\documentclass[10pt,DIV=12,a4paper,numbers=noenddot]{scrreprt}

\usepackage{tabu}
\usepackage{longtable}
\usepackage{etoolbox}

\makeatletter
\newcommand{\changefont}[3]{\usefont{\f@encoding}{#1}{#2}{#3}}
\patchcmd{\LT@makecaption}{\hss}{\hss\normalfont\normalsize}{}{}
\makeatother
\newenvironment{myfont}[6][1]
{\par}

\begin{document}

\begin{myfont}{phv}{m}{n}{6}{9} % Change font to Helvetica
\begin{longtabu}{ll}

Longtable & Longtable \\
Longtable & Longtable \\
Longtable & Longtable \\
Longtable & Longtable \\
Longtable & Longtable \\
\caption{This is a caption}
\end{longtabu}
\end{myfont}

This is text after the long table and shoud be in standard latex format again. Apparently,
it is not as you can see from the baseline stretch which seems not to be 1.2. However, the
font type seems to be ok.

\end{document}


Notice the use of \linespread{1.2} rather than \renewcommand{\baselinestretch}{1.2}. However it's preferable to use the setspace package and its \setstretch{1.2} command.

I've redefined your \changefont command in a possibly clearer way using the standard \usefont command. In \f@encoding the current encoding is stored.

The myfont environment can be used anywhere. It has five mandatory arguments: family, series, shape, size and baselineskip and an initial optional argument, the baseline stretch (default 1).

I've also patched the \caption command inside a longtable (or longtabu which uses the same) so that it's typeset in normal font and size.

-
Hi egreg, thanks a lot for this solution. There are 2 things I want to ask you. 1) what's the difference between \linespread and setstretch and why is it better than the baselinestretch command I used? When I change \linespread to \setstretch, in your code, can I simpl replace \par\linespread{#1} with \par\setstretch{#1}? 2) I am wondering how I can set the initial optional argument (the baselinestretch) to 1.2? Apparently, the line spread{1.2} sets it to 1.2 but this is overwritten by your code and set back to one within the \begin{my font} environment. I can I set this optional param to 1.2? – Josh Feb 6 '13 at 14:32
@Josh If you use setspace, just replace \linespread with \setstretch. It's preferable because it does some more things which help in limiting the awful result of increasing the interline space so much. If you replace [6][1] in the definition of myfont with [6][1.2] you'll have that the default value of the optional argument is 1.2 – egreg Feb 6 '13 at 14:39
Thanks a lot. this is fantastic! I need to incorporate this now into my actual latex environment but everything seems to work so far. I let you know after I migrated that if it works or not. Thumbs crossed but I think it will. In case I have some troubles I hope I can come back to you. Sorry, just one more thing for now. Is the toolbox package you used needed to create this new myfont function? – Josh Feb 6 '13 at 14:48
Hi egreg, I just realized that whenever I use caption now that the baseline stretch is not 1.2 but just 1. The font apparently as we can see from your image is the standard latex font but since the caption is very short I did not realize that the line space is now 1 and not 1.2 anylonger. I guess this is because you patched the caption in your code. I have no idea how to changes this back unfortunately. – Josh Feb 6 '13 at 15:42
One of the workings of setspace is to reduce the interline space to normal in some situations. So large a baseline skip is, in my opinion awful; a caption with enlarged baseline skip is even more awful. – egreg Feb 6 '13 at 15:49