Combining \command, \command{} and \begin{environment}\end{environment} in \newenvironment

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]%%
}


You could use the environ package version of environment which makes the body of the environment available as \BODY to get it all in the argument but I'd be wary of using \afterpage in a production environment (see the package documentation).

I don't think you need that here just use sidewaystable from rotating

\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,rotating}
\usepackage{calc}

% force page 3 to rotate in the view
% (a better interface to this could be added)

\makeatletter
\let\old@outputpage\@outputpage
\def\@outputpage{%
% add as many lines like this as needed
\ifnum\value{page}=3 \pdfpageattr\expandafter{\the\pdfpageattr /Rotate 90 }\fi
\old@outputpage}
\makeatother

\begin{document}

\lipsum[1-10]

\begin{sidewaystable}
\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}
\end{sidewaystable}

\lipsum[1-10]

\end{document}

• Thank you. Are there also options to automatically rotate the respective pages in the pdf-file so that it is readable horizontally? In some relevant settings, it is required to have pdf-files that display writing always horizontally, so when read on a screen the reader never has to tilt his head or turn the page. Otherwise, I would have to rotate the pages manually using a pdf-editor. – Marie. P. Dec 14 '16 at 17:30
• @Marie.P. ooh good idea, no actual option but I added some low level code to do that, i may see if I can add something to rotating to make that a bit more automatic. – David Carlisle Dec 14 '16 at 19:42
• I appreciate your showing me a different way. The rotation is still just manual though? Just for future reference, can you elaborate on how a command of the form \command{} (with content in curly brackets, as above afterpage) and \command (without any brackets) would be included in the newenvironment? Or is it not possible at all and I must opt for using that other environment-editor you had in your answer (with the \BODY command)? – Marie. P. Dec 15 '16 at 11:12
• @Marie.P. standard environment doesn't make the content available to pass to a macro such as afterpage, you need to use environ package version of environment definition or equivalent to define it. – David Carlisle Dec 15 '16 at 11:14
• @Marie.P. yes in principle you could have a scheme that (internally) has a \label\pagref to the floats and then automatically added the page attributes on the pages referenced by pageref but not today... – David Carlisle Dec 15 '16 at 13:03