# Text alignment in clipped parbox

I have the following problem: I need to put variable length content into a fix-sized parbox the gets clipped with clipbox command. And want the content to be vertically centered. As long as the content fits into the parbox everthing is fine and it works. But if the text is longer then the available space, the vertical center alignment in combination with the clipbox command causes my text to be cropped. Please see the image below.

This is the code that demonstrates the problem:

\documentclass{scrartcl}

\usepackage{calc}
\usepackage{pstricks}

\begin{document}
\begin{pspicture}(0,0)(\textwidth,\textheight)
\newlength{\yPosition}
\setlength{\yPosition}{\textwidth-5cm}
\rput[lt](2cm,\yPosition){%
\fbox{
\clipbox{%
\parbox[t][1cm][c]{5cm}{%
short text
}%
}%
}
}
\rput[lt](2cm,\yPosition){%
\fbox{
\clipbox{%
\parbox[t][1cm][c]{5cm}{%
% short text
long text long text long text long text long text long text long text long text
}%
}%
}
}
\end{pspicture}
\end{document}


So, my question is: Is it possible to have the parbox content top-aligned if the content exceeds the size and center alignment if the content fits?

• Do you mind making that a full minimal example? We cannot just copy a try that sniplet without adding to it. – daleif Mar 25 '15 at 14:16
• Sorry, I completed the example. – nis Mar 25 '15 at 14:29
• Pas, I never use pstricts. BTW: you might want to label this as pstricks as well, might attract more people. – daleif Mar 25 '15 at 14:37
• Is it the \parbox height to adapt to the contents that you want? – Bernard Mar 25 '15 at 16:14
• The content of the parbox comes from a PHP script and I have no influence on its length except limiting the size of the box and crop everything that's too long to avoid overlapping with other contents on the page. Does this answer your question? – nis Mar 25 '15 at 16:18

I got it working using the below code. I altered the inner parbox position to top and filled the space before and after the text with \vspace*{\fill}. This way the short text is vertically centered and the long text top aligned and cropped correctly.

\documentclass{scrartcl}

\usepackage{calc}
\usepackage{pstricks}

\begin{document}
\begin{pspicture}(0,0)(\textwidth,\textheight)
\newlength{\yPosition}
\setlength{\yPosition}{\textwidth-5cm}
\rput[lt](2cm,\yPosition){%
\fbox{
\clipbox{%
\parbox[t][1cm][t]{5cm}{%
short text
}%
}%
}
}
\rput[lt](2cm,\yPosition){%
\fbox{
\clipbox{%
\parbox[t][1cm][t]{5cm}{%
\vspace*{\fill}
long text long text long text long text long text long text long text long text
\vspace*{\fill}
}%
}%
}
}
\end{pspicture}
\end{document}