I want to create a new environment, called ltable, which takes very wide tables (tabularx-environments, which are given output from statistical software), puts a table environment around the tabularx (so the tables are counted and listed in the list of tables etc.) and rotates the page using pdflscape.
This works fine, but I cannot get text to float around the rotated page. The previous page of text is half-empty, then the following page is rotated 90° and contains the ltable, and then text resumes again on the next page.
I then tried to use \afterpage{} and \clearpage to force a float, following some ideas from here.
In the following, you can see how I tried to create the environment ltable as follows:
\newenvironment{ltable}
{ \afterpage \clearpage
\begin{landscape}%
\begin{table}}
{\end{table}%
\end{landscape}}
so that the ltable-environment should be equivalent to the following (note that I do not know how to differentiate between \clearpage having no curly brackets, but \afterpage{} having them while defining the environment, which is probably the source of error):
\afterpage{
\clearpage
\begin{landscape}
\begin{table}
...
\end{table}
\end{landscape}
}
If you compile the following MWE, you will see that the text does not float around the table as desired.
First, non-floating MWE:
\documentclass{article}
\usepackage{geometry}
\geometry{top=1in, bottom=1in, left=1in, right=1in}
\usepackage{lipsum}% for dummy text
\usepackage{afterpage}
\usepackage{pdflscape}
\usepackage{tabularx}
\usepackage[small,bf]{caption}
\usepackage{graphicx}
\usepackage{calc}
\newenvironment{ltable}
{ \afterpage \clearpage
\begin{landscape}%
\begin{table}}
{\end{table}%
\end{landscape}}
\begin{document}
\lipsum[1-10]
%\afterpage{%
% \clearpage% To flush out all floats, might not be what you want
\begin{ltable}
\makebox[\linewidth][c]{
\begin{tabularx}{\linewidth}{l|X}
\hline \hline
Variable & Description\\ \hline
\emph{lntotalassets}&natural logarithm of total assets\\
%nepro & net income per gross profit\\
\hline \hline
\end{tabularx}
}
% must not include verbatim content
\end{ltable}
%}
\lipsum[1-10]
\end{document}
However, when I manually add the commands \afterpage{} and \clearpage around ltable, while having ltable consist only of landscape and table, as in the second MWE, it does work.
Second floating MWE:
\documentclass{article}
\usepackage{geometry}
\geometry{top=1in, bottom=1in, left=1in, right=1in}
\usepackage{lipsum}% for dummy text
\usepackage{afterpage}
\usepackage{pdflscape}
\usepackage{tabularx}
\usepackage[small,bf]{caption}
\usepackage{graphicx}
\usepackage{calc}
\newenvironment{ltable}
{% \afterpage \clearpage
\begin{landscape}%
\begin{table}}
{\end{table}%
\end{landscape}}
\begin{document}
\lipsum[1-10]
\afterpage{%
\clearpage% To flush out all floats, might not be what you want
\begin{ltable}
\makebox[\linewidth][c]{
\begin{tabularx}{\linewidth}{l|X}
\hline \hline
Variable & Description\\ \hline
\emph{lntotalassets}&natural logarithm of total assets\\
%nepro & net income per gross profit\\
\hline \hline
\end{tabularx}
}
% must not include verbatim content
\end{ltable}
}
\lipsum[1-10]
\end{document}
How can I put the \afterpage{} and \clearpage (without curly brackets!) into the \newenvironment, to have a command as short and concise as in MWE 1?
Own solution: Ignoring comments saying to be careful with afterpage, I came up with the following code to define the environment wtable that does what I want it to do (preamble and usedpackage as above, plus the package environ):
\def\afterpagebody#1[#2]{%
\afterpage{ \clearpage \begin{landscape} \begin{table}[#2]
#1%
\end{table} \end{landscape}
}}
\NewEnviron{wtable}[1][ht]{%
\expandafter\afterpagebody\expandafter{\BODY}[#1]%%
}