\documentclass[a4paper]{article}
\usepackage{environ}
\makeatletter
\newenvironment{varquotation}
{\Collect@Body\varquote\ignorespaces}
{\ignorespacesafterend}
\makeatother
\newcommand{\varquote}[1]{%
\setbox0=\vbox{
\quotation\em#1\endquotation
\expandafter}%
\ifnum\prevgraf>3
\begin{quotation}\em#1\end{quotation}
\else
\emph{#1}%
\fi}
\begin{document}
Some text before the quotation
Some text before the quotation
Some text before the quotation
\varquote{This is a short quotation}
Some text before the quotation
Some text before the quotation
Some text before the quotation
\varquote{And this is a longer quotation
longer quotation
longer quotation
longer quotation
longer quotation
longer quotation
longer quotation
longer quotation
longer quotation
longer quotation
longer quotation
longer quotation
longer quotation
longer quotation
longer quotation
longer quotation}
that should be displayed.
Some text before the quotation
Some text before the quotation
\begin{varquotation}
This is a short quotation
\end{varquotation}
Some text before the quotation
Some text before the quotation
Some text before the quotation
Some text before the quotation
\begin{varquotation}
And this is a longer quotation
longer quotation
longer quotation
longer quotation
longer quotation
longer quotation
longer quotation
longer quotation
longer quotation
longer quotation
longer quotation
longer quotation
longer quotation
longer quotation
longer quotation
longer quotation
\end{varquotation}
that should be displayed.
\end{document}
There are two forms, choose the one you prefer: for obviously short quotation the "in-line" form \varquote{text}
might be better; in any case you can use the environment form. You'll have to customize the quotation
environment to respect other requirements.
Explanation. When TeX makes a paragraph, it records the number of lines in the internal parameter \prevgraf
. So I typeset the quotation in a box in order to compute the number of lines if it were displayed. If this number is greater than 3 the quotation will be displayed, otherwise it will be set in-line.
The \expandafter
before the closing brace of the \vbox
uses the just established value of \prevgraf
in the conditional, so it's impossible to use the built \box0
which would give different results anyway even if the code were
...
\quotation\em#1\endquotation
\global\chardef\nlines=\prevgraf}
\ifnum\nlines>3
\begin{quotation}\em#1\end{quotation}
...
The "shorter" version avoids a global assignment.