# Custom quotation style which depends on length of quotation

I am writing my PhD-thesis and I need to meet certain formatting requirements in regard to quotations. I know there is the quote environment. But in this conditions it doesn't match my universities demands. I also know that there is the possibility to redefine the environment. But I don't dare.

1. quotations have to be in italics
2. a) if the quotation exceeds three lines it need to be indented
b) otherwise it has to integrate into the text (basically the up-to-three-lines version should look like this: blabla bla \emph{quotation} bla bla.)

Does anyone know how to redefine the command in a way that it includes 1. & 2. that would be great.

-

You could simply test if the argument is longer than three times the line width when set inside box.

\documentclass{article}

\makeatletter
\newcommand{\cquotation}[1]{%
% \settowidth doesn't like paragraphs
\setbox\@tempboxa\hbox{%
\def\par{\hspace{3\linewidth}}% If a paragraph is included force long form
%\let\par\space  % Ignore paragraphs
#1}%
\ifdim\wd\@tempboxa>3\linewidth
\begin{quote}
\itshape
#1
\end{quote}
\else
{\itshape #1}%
\fi
}
\makeatother

\begin{document}

\def\Text{text text text text text }
\edef\Text{\Text\Text\Text\Text\Text}

\Text
\cquotation{some short quote}
\Text

\Text
\cquotation{%
Some quotation just short of three lines of text.
Some quotation just short of three lines of text.
Some quotation just short of three lines of text.
Some quotation just short of three lines of text.
Some quotation just short of three lines.
}
\Text

\Text
\cquotation{%
Some quotation shorter than three lines.

But with a paragraph inside.
}
\Text

\Text
\cquotation{%
Some quotation longer than three lines.  Some quotation longer than three lines.
Some quotation longer than three lines.  Some quotation longer than three lines.
Some quotation longer than three lines.  Some quotation longer than three lines.
Some quotation longer than three lines.  Some quotation longer than three lines.
}
\Text

\end{document}

\def\Text{text text text text text }
\edef\Text{\Text\Text\Text\Text\Text}

\Text
\cquotation{some short quote}
\Text

\Text
\cquotation{%
Some quotation just short of three lines of text.
Some quotation just short of three lines of text.
Some quotation just short of three lines of text.
Some quotation just short of three lines of text.
Some quotation just short of three lines.
}
\Text

\Text
\cquotation{%
Some quotation longer than three lines.  Some quotation longer than three lines.
Some quotation longer than three lines.  Some quotation longer than three lines.
Some quotation longer than three lines.  Some quotation longer than three lines.
Some quotation longer than three lines.  Some quotation longer than three lines.
}
\Text

\end{document}


-
Hi Martin, thanks for your help. This version perfectly works. It just took some time to understand. But now I got it. So thank you very much. –  Philip May 25 '11 at 11:31
With \lipsum* you don't get a final \par. –  egreg May 25 '11 at 11:58
To be sure it never exceeds 3 lines of text (even after the space expansion) you can use a bit smaller factor like 2.8 since after indentation, it will surely be more than 3 lines ;) –  tohecz Nov 27 '12 at 11:03
\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.

-
Hi, thank you very much for your help. But there remains one problem. When I try to typeset the code you provided the console refuses to generate a pdf. It obviously has some problem with the \begin{varquotation}. –  Philip May 25 '11 at 11:24
@Philip: what's precisely the error message? –  egreg May 25 '11 at 11:57
It stops to generate the pdf-file and writes this: l. 46 \begin{varquotation} ? –  Philip May 25 '11 at 12:40
@Philip: strange, because it works for me. That's not the error message, however. –  egreg May 25 '11 at 12:51
I dont know why it didnt work, but know it does. So thank you very much. –  Philip May 25 '11 at 15:40