# [pstricks] add latex text (and code) in pstricks

Tue Aug 8 21:54:44 CEST 2006

tiM Sportny a écrit :
> hello,
>
> just started using pstricks two days ago and skipped one night of sleep
> using it. Not that it's hard to use, just because it's too much fun to see
> all possibilities.

Isn't it :-)
I understand: I have skipped many nights of sleep too!

> But there's one i can't find, don't know if it's possible but lets try:

*All* is possible.

> I'm looking for a way to make a function/environment in latex where a
> paragraph (or two) can be printed and the pstricks picture will adjust it's
> height to it.

It's not a pstricks question. For this kind of problem, you can put the
body of the environment in a box, using environment lrbox, and make what
you want in the final part of your environment.

> For example make the following code:
>
> \begin{mypsenv}
>     just some example text that is used to illustrate my plans
> \end{mypsenv}
>
> print the text of the environment and place it in a gray box with rounded
> lower corners.
>
> One way i came up with was something like this:
>
> \newenvironment{mypsenv}[0]{\begin{pspicture}(\linewidth,-1)
> \psline*[linearc=.5,linecolor=gray](0,0)(0,-\parheight)(\linewidth,-\parheight)(\linewidth,0)
> \end{pspicture}}{\vspace{1cm}}
>
> where \parheight would reprecent the total height of the text in the
> environment. There is one majar drawback, i couldn't find a way to determine
> the height of the text in the environment (\parheight).

With box, it's \ht\nameofbox+\dp\nameofbox.

> Does somebody know the way to do this kind of things? I prefer to let latex
> do the typesetting itself

It's a good idea.

> so i can use latex tables or something in it, but
> if someone has a solution that fits just for text i'd be a great help to.

Ok. Let's go. I use \psframesep in order to have separation between text
and frame. For calculation, the calc package is very nice.
In this kind of problem, the hardest part is to deal vertical and
horizontal dimensions in a correct way.

\documentclass{article}
\usepackage[T1]{fontenc}
\usepackage[latin1]{inputenc}
\usepackage{lmodern}
\usepackage{pstricks}
\usepackage{calc}
\usepackage{lipsum}
\usepackage[english]{babel}

\pagestyle{empty}

\newsavebox{\mypsbox}
\newlength{\parheight}
\newlength{\parwidth}
\newenvironment{mypsenv}[1][\linewidth]
{%
% \parheight = vertical space before frame.
% -0.3\baselineskip is onky a proposition, you can
% play with other values.
\setlength{\parheight}{-0.3\baselineskip+2\psframesep}
\par\vspace{\parheight}\noindent
% \parwidth = whole width, that is, width of frame.
% #1 don't exist in the final part, \parwidth will exist.
\setlength{\parwidth}{#1}%
\begin{lrbox}{\mypsbox}
% material typesetted in a minipage
% Note that the material is shorter than the frame.
\begin{minipage}[t]{#1-\psframesep*2-\pslinewidth*2}
}
{%
\end{minipage}%
\end{lrbox}%
% \parheight = total height (of minipage)
\setlength{\parheight}{\ht\mypsbox+\dp\mypsbox}%
% no problem for width.
% about height, there will be a shifting relative to
% minipage (look the code carefully)
\psline*[linearc=.5,linecolor=gray]
(0,2\psframesep)(0,-\parheight)
(\parwidth,-\parheight)(\parwidth,2\psframesep)
% minipage shifted by \psframesep.
\rput[tl](\psframesep,\psframesep){\usebox{\mypsbox}}
\par\vspace{\parheight}
}

\begin{document}
Some text above.
\begin{mypsenv}[0.5\linewidth]
\lipsum[1]
\end{mypsenv}
Some text below.

Second trial:
\psset{framesep=0pt}
\begin{mypsenv}[0.5\linewidth]
\lipsum[2]
\end{mypsenv}
Some text below.

The last one:
\psset{framesep=12pt}
\begin{mypsenv}[\linewidth]
\lipsum[1-2]
\end{mypsenv}
Some text below.

\end{document}

In this code, mypsenv works without pspicture. If you put mypsenv in a
pspicture environment, you have to remove \par(s) and \vspace(s) (an
\noindent).

Hope this help,

Jean-Côme Charpentier