The semantics of a given environment require that it is typeset inside a minipage if it is used inside a tabular. If I add the minipage declaration for a positive check for "internal" mode in the environment using \ifinner, it works: the minipage is added only if in tabular mode. (This can be seen in the output because the minipage does not expand over the full text width.) I have the following questions:
- Why does the check for inner mode succeed when the
kframeenvironment is closed? Is TeX in "internal" mode inside aminipageenvironment? - Is there a more reliable way to do this detection? Is the detection correct at all?
The kframe environment is used in code generated by knitr; it should work both inside and outside of a tabular.
\documentclass{article}
\pagestyle{empty}
\usepackage{color}
\usepackage{framed}
\newenvironment{kframe}{%
% The \ifinner check below is necessary
\ifinner\begin{minipage}{.8\textwidth}\else\fi%
\def\FrameCommand##1{\colorbox{yellow}{##1}}%
\MakeFramed{}}{%
\endMakeFramed%
% Why does this \ifinner check work?
\ifinner\end{minipage}\fi}
\begin{document}
\begin{kframe}
Any text long enough will do here.
Any text long enough will do here.
Any text long enough will do here.
\end{kframe}
\begin{tabular}{l}
\begin{kframe}
Any text long enough will do here.
Any text long enough will do here.
Any text long enough will do here.
\end{kframe}
\end{tabular}
\end{document}

\ifinneris true in internal vertical mode (so also inside aminipage), restricted horizontal mode (so also inside atabular), in inline math mode and in any subformula in display math mode. – egreg Jun 20 '12 at 12:01