How to make table floats work with preview/standalone environment?

Couldn't make a table float work inside the preview environment:

\documentclass[fontsize=16pt]{scrartcl}

\usepackage[active,tightpage]{preview}
\setlength{\PreviewBorder}{25pt}

\begin{document}
\begin{preview}

\begin{table}
\centering
\begin{tabular}{c|c}
a & b\\
\hline
c & d\\
\end{tabular}
\caption{testing}
\label{tab:testing}
\end{table}

\end{preview}
\end{document}


gives error "Not in outer par mode". The manual of preview mentions a "floats" option, but adding it makes no difference. I know that making a table "float" don't make much sense in a single preview page, I just want them to become non-float and stay in the place where it's defined and show up in the preview.

And then I found this question. It says that standalone package would work. But I couldn't get it to work either:

\documentclass{standalone}

\begin{document}

\begin{table}
\centering
\begin{tabular}{c|c}
a & b\\
\hline
c & d\\
\end{tabular}
\caption{testing}
\label{tab:testing}
\end{table}

\end{document}


gives error "Something's wrong--perhaps a missing \item". And even if it works, it raises a serious problem, that is, it occupies the documentclass but I must use KOMA-Script. By the way, I use xelatex, don't know if it matters.

Any ideas?

Edit: I think I have mentioned in the post that I know what a "float" means and that it does not make "much" sense. But it does make some sense: I use preview for, literally, preview. So every element in the document should show up in the preview so I can "preview" it. And for a table float the proper way of show up is to become a non-float and stay at where it's defined. This is exactly what this post says standalone is supposed to do. Then after the preview, if I comment out the preview package or make it inactive, every thing should work in the normal way again (table floats float normally, etc). So I'm not asking for a method of making a table float sticky, but how to make it show up in preview/standalone environment.

Solution:The solution turns out to be

\documentclass[preview,class=scrartcl,fontsize=20pt]{standalone}


This will automatically turn table floats into non-float while retaining the original document class. Also see Jesse's answer for a clever but a little more sophisticated solution.

• table is a float and using it like this doesn't make sense. Use float package and make \begin{table}[H] or don't use table. To have captions use capt-of package with it \captionof macr. – user11232 Apr 17 '14 at 7:16
• @HarishKumar It does make some sense. Please see my updated question. – kawing-chiu Apr 17 '14 at 9:11

Update

In the link you have posted, the author of the standalone package mentions that floats are now possible in standalone and that one should have a look into the manual on the float option.

Doing this for you, I got the following code, which should work for you.

Edit I also included the required option to get the scrartcl-look and the desired fontsize.

% arara: pdflatex

\documentclass[%
,float=false % this is the new default and can be left away.
,preview=true
,class=scrartcl
,fontsize=20pt
]{standalone}

\begin{document}

\begin{table}
\centering
\begin{tabular}{c|c}
a & b\\
\hline
c & d\\
\end{tabular}
\caption{testing}
\label{tab:testing}
\end{table}

\end{document}


You should not use floating environments in the standalone class, as it makes no sense. Labels make no sense either. I would recommend to use the caption package for your caption (as this package is often used in normal articles as well) and to wrap you table in a minipage.

% arara: pdfatex

\documentclass{standalone}
\usepackage{caption}

\begin{document}
\minipage{0.2\textwidth}
\centering
\begin{tabular}{c|c}
a & b\\
\hline
c & d\\
\end{tabular}
\captionof{table}{testing}
\endminipage
\end{document}

• Floats in preview do make some sense. Please see my updated question. And so do labels. I sometimes test hyperlinks in preview, if they works in preview, they works outside preview, too. And you need links when reading a really long preview page. – kawing-chiu Apr 17 '14 at 9:17
• @kawing-chiu Edited my answer. – LaRiFaRi Apr 17 '14 at 10:26
• Thanks very much for the update, it's quite close now. So how can I retain \documentclass[fontsize=16pt]{scrartcl} when using standalone as a documentclass? – kawing-chiu Apr 17 '14 at 12:56
• @kawing-chiu Sorry, I don't understand that. You have to choose between document classes. Or is Jesses approach, what you want? He ha got kind of a switch. If you wan't, I can program a real switch, but when do you want to switch the document class? – LaRiFaRi Apr 17 '14 at 14:00
• I've figured it out. \documentclass[preview,class=scrartcl,fontsize=20pt]{standalone} will do the trick. The standalone package has the ability to use a "underlying package". Maybe you can include this in your answer. – kawing-chiu Apr 17 '14 at 14:42

An alternative is to define an non-float environment for tablehere, but still capable of caption ability without using package.

Update: tablehere is changed to the table, but renew it to non-float when preview mode is used. So remove all % below for preview mode.

\documentclass[fontsize=16pt]{scrartcl}

%\usepackage[active,tightpage]{preview}
%\setlength{\PreviewBorder}{25pt}
%
%\makeatletter
%\renewenvironment{table}
%{\def\@captype{table}}
%{}
%\makeatother

\begin{document}
%\begin{preview}

\begin{table}
\centering
\begin{tabular}{c|c}
a & b\\
\hline
c & d\\
\end{tabular}
\caption{testing}
\label{tab:testing}
\end{table}

%\end{preview}
\end{document}


\makeatletter
\newenvironment{tablehere}
{\def\@captype{table}}
{}
\makeatother


Code

\documentclass[fontsize=16pt]{scrartcl}

\usepackage[active,tightpage]{preview}
\setlength{\PreviewBorder}{25pt}
\makeatletter
\newenvironment{tablehere}
{\def\@captype{table}}
{}
\makeatother
\begin{document}
\begin{preview}

\begin{tablehere}
\centering
\begin{tabular}{c|c}
a & b\\
\hline
c & d\\
\end{tabular}
\caption{testing}
\label{tab:testing}
\end{tablehere}

\end{preview}
\end{document}

• Hi, thanks for the answer. It kind of works, but what I really want is to make it work under preview/standalone without the need to change the original float (see my updated question). Can it be done? – kawing-chiu Apr 17 '14 at 9:32
• @kawing-chiu -- please see my update. – Jesse Apr 17 '14 at 11:54