Re: [Risolto] Non so come ricreare lo stile in figura

#17365
Up
0
Down
::


Ciao a tutti.

So che la domanda da me sollevata precedentemente in questo thread ha ricevuto la risposta: “è perfettamente inutile”. Tuttavia ho ritenuto istruttivo per me indagare in merito, cioè studiare il codice di framed.sty, e ritengo di essere riuscito a fare ciò che avevo in mente (che sia una cosa utile o non credo che interessi meno della soluzione del problema).

Avevo ed ho l’esigenza di inserire delle scritte del tipo “(continua alla pag. successiva)” oppure “(continua dalla pag. precedente)” in concomitanza di boxes con background colorato che prendessero più di un salto pagina.

Ho ottenuto il risultato seguente:


non senza modificare (ahimè) il pacchetto framed di Donald Arseneau.
Allego il codice di “myframed.sty”:
`
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% This is myframed.sty, a slight modification of
% framed.sty v 0.8a, 21-Jul-2003, by Donald Arseneau.
% Author: Agostino De Marco
% Note: modifications to the riginal code are enclosed by the comments:
% % agodemar…
%
% % … agodemar
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\ProvidesPackage{myframed}[2003/07/21 v 0.8a:
framed or shaded text with page breaks]

\newenvironment{framed}% using default \FrameCommand
{\MakeFramed {\advance\hsize-\width \FrameRestore}}%
{\endMakeFramed}

\newenvironment{shaded}{%
\def\FrameCommand{\colorbox{shadecolor}}%
\MakeFramed {\FrameRestore}}%
{\endMakeFramed}

\newenvironment{leftbar}{%
\def\FrameCommand{\vrule width 3pt \hspace{10pt}}%
\MakeFramed {\advance\hsize-\width \FrameRestore}}%
{\endMakeFramed}

\chardef\FrameRestore=\catcode`\| % for debug
\catcode`\|=\catcode`\% % (debug: insert space after backslash)

\def\MakeFramed#1{\par
% measure added width and height; call result \width and \height
\setbox\z@\vbox{\vskip-1in \hbox{\hskip-1in
\FrameCommand{\hbox{\vrule \@height .7in \@depth.3in \@width 1in}}}%
\vskip\z@skip}%
\def\width{\wd\z@}\def\height{\ht\z@}%
\edef\fb@frw{\the\width}\edef\fb@frh{\the\height}%
% insert pre-penalties and skips
\begingroup
\skip@\lastskip
\if@nobreak\else
\penalty9999 % updates \page parameters
\ifdim\pagefilstretch=\z@ \ifdim\pagefillstretch=\z@
\edef\@tempa{\the\skip@}%
\ifx\@tempa\zero@glue \penalty-30
\else \vskip-\skip@ \penalty-30 \vskip\skip@
\fi\fi\fi
\penalty\z@
% Give a stretchy breakpoint that will always be taken in preference
% to the \penalty 9999 used to update page parameters. The cube root
% of 10000/100 indicates a multiplier of 0.21545, but the maximum
% calculated badness is really 8192, not 10000, so the multiplier
% is 0.2301.
\advance\skip@ \z@ plus-.5\baselineskip
\advance\skip@ \z@ plus-.231\height
\advance\skip@ \z@ plus-.231\skip@
\advance\skip@ \z@ plus-.231\topsep
\vskip-\skip@ \penalty 1800 \vskip\skip@
\fi
\addvspace{\topsep}%
\endgroup
% clear out pending page break
\penalty\@M \vskip 2\baselineskip \vskip\height
\penalty9999 \vskip -2\baselineskip \vskip-\height
\penalty9999 % updates \pagetotal
|\message{After clearout, \pagetotal=\the\pagetotal, \pagegoal=\the\pagegoal. }%
\fb@adjheight
\setbox\@tempboxa\vbox\bgroup
#1% Modifications to \hsize (can use \width and \height)
\textwidth\hsize \columnwidth\hsize
}

\def\endMakeFramed{\par
\kern\z@ \penalty-100 % put depth into height
\egroup
\begingroup \put@frame \endgroup
}

% \put@frame takes the contents of \@tempboxa and puts all, or a piece,
% of it on the page with a frame (\FrameCommand). It recurses until
% all of \@tempboxa has been used up. (\@tempboxa must have zero depth.)

\def\put@frame{\relax
\ifdim\pagegoal=\maxdimen \pagegoal\vsize \fi
| \message{=============== Entering putframe ====================^^J
| \pagegoal=\the\pagegoal, \pagetotal=\the\pagetotal. }%
\ifinner \else
\dimen@\pagegoal \advance\dimen@-\pagetotal % natural space left on page
\ifdim\dimen@<2\baselineskip
| \message{Page has only \the\dimen@\space room left; eject. }%
\eject \fb@adjheight \put@frame
\else % there's appreciable room left on the page
| \message{\string\pagetotal=\the\pagetotal,
| \string\pagegoal=\the\pagegoal,
| \string\pagestretch=\the\pagestretch,
| \string\pageshrink=\the\pageshrink,
| \string\fb@frh=\fb@frh. \space}
| \message{Box of size \the\ht\@tempboxa\space + \fb@frh}%
\begingroup % temporarily set \dimen@ to be…
\advance\dimen@.8\pageshrink % maximum space available on page
\advance\dimen@-\fb@frh\relax % space available for frame's contents
\expandafter\endgroup
% restore \dimen@ to real room left on page
\ifdim\dimen@>\ht\@tempboxa % whole box does fit
| \message{fits in \the\dimen@. }%
\else % box must be split
| \message{must be split to fit in \the\dimen@. }%
\setbox\@tempboxa\vbox{% simulate frame and flexiblity of the page:
\vskip \fb@frh \@plus\pagestretch \@minus.8\pageshrink
\kern137sp\kern-137sp\penalty-30
\unvbox\@tempboxa}%
\edef\fb@resto@set{\boxmaxdepth\the\boxmaxdepth \splittopskip\the\splittopskip}%
\boxmaxdepth\z@ \splittopskip\z@
\setbox\tw@\vsplit\@tempboxa to\dimen@
\setbox\tw@\vbox{\unvbox\tw@}% natural-sized
| \message{Box of size \the\ht\@tempboxa\space split to \the\dimen@.
| Natural height of split box is \the\ht\tw@. }%
% If the split-to size > (\vsize-\topskip), then set box to full size
\begingroup
\advance\dimen@\topskip
\expandafter\endgroup
\ifdim\dimen@>\pagegoal
| \message{Frame is big — Use up the full column. }%
\dimen@ii\pagegoal
\advance\dimen@ii -\topskip
\advance\dimen@ii \FrameHeightAdjust\relax
\else % suspect this is wrong:
% If the split-to size > feasible room_on_page, rebox it smaller.
\advance\dimen@.8\pageshrink
\ifdim\ht\tw@>\dimen@
| \message{Box too tall; rebox it to \the\dimen@. }%
\dimen@ii\dimen@
\else % use natural size
\dimen@ii\ht\tw@
\fi
\fi
% Re-box contents to desired size \dimen@ii
\advance\dimen@ii -\fb@frh
\setbox\tw@\vbox to\dimen@ii \bgroup
% remove simulated frame and page flexibility:
\vskip -\fb@frh \@plus-\pagestretch \@minus-.8\pageshrink
\unvbox\tw@ \unpenalty\unpenalty
\ifdim\lastkern=-137sp % whole box went to next page
| \message{box split at beginning! }%
\egroup \fb@resto@set \eject % (\vskip for frame size was discarded)
\fb@adjheight
\else %
\egroup \fb@resto@set
\ifvoid\@tempboxa % it all fit after all
| \message{box split at end! }%
\setbox\@tempboxa\box\tw@
\else % it really did split
| \message{box split as expected. Its reboxed height is \the\ht\tw@. }%
\ifdim\wd\tw@>\z@
%\centerline{\FrameCommand{\box\tw@}}% ??? \centerline bad idea
%
% agodemar …
\centerline{\FrameCommand{\box\tw@}%
\if@twoside\ifodd\c@page%
%\par\vskip -\baselineskip
%\medskip%\medskip
%\vskip -1pt
\makebox[0pt][r]{%
\raisebox{-0.8\baselineskip}[0pt][0pt]%
{\scriptsize\itshape(continua alla pagina seguente)}}%
\fi\fi
}% ??? \centerline bad idea
% … agodemar
% agodemar … (experimental
\setbox\@tempboxa\vbox{%
\if@twoside\ifodd\c@page%
\hskip-\parindent
\hskip-\FrameSep
\makebox[0pt][l]{%
\raisebox{0.5\baselineskip}[0pt][0pt]%
{\scriptsize\itshape(continua dalla pagina precedente)}}%
\par\vskip-\baselineskip
\fi\fi%
\@tempboxa\unvbox\@tempboxa}%
% … agodemar
\else
| \message{Zero width means likely blank. Don't frame it (guess)}%
\box\tw@
\fi
\hrule \@height\z@
\eject
\fb@adjheight
\put@frame
\fi\fi\fi\fi\fi
\ifvoid\@tempboxa\else
\centerline{\FrameCommand{\box\@tempboxa}}%
\nointerlineskip \null %{\showoutput \showlists}
\penalty-30 \vskip\topsep
\fi}

\def\fb@adjheight{%
\vbox to\FrameHeightAdjust{}% get proper baseline skip from above.
\penalty\@M \nointerlineskip
\vskip-\FrameHeightAdjust
\penalty\@M} % useful for tops of pages

\edef\zero@glue{\the\z@skip}

\catcode`\|=\FrameRestore

% Provide configuration commands:
\providecommand\FrameCommand{\fboxrule=\FrameRule \fboxsep=\FrameSep \fbox}
\@ifundefined{FrameRule}{\newdimen\FrameRule \FrameRule=\fboxrule}{}
\@ifundefined{FrameSep} {\newdimen\FrameSep \FrameSep =3\fboxsep}{}

% Height of frame above first baseline when frame starts a page:
\providecommand\FrameHeightAdjust{6pt}

% \FrameRestore has parts of \@parboxrestore. See how it is used in the
% “settings” argument of \MakeFrame. Previous behavior can be restored by
% using \@parboxrestore there, or redefining:
% \makeatletter \renewcommand\FrameRestore{\@parboxrestore} \makeatother
\def\FrameRestore{%
\let\if@nobreak\iffalse
\let\if@noskipsec\iffalse
% \let\par\@@par ??
\let\-\@dischyph
\let\'\@acci\let\`\@accii\let\=\@acciii
% \parindent\z@ \parskip\z@skip Definitely omit!
% \everypar{}% ??
\linewidth\hsize
% \@totalleftmargin\z@
% \leftskip\z@skip \rightskip\z@skip \@rightskip\z@skip
% \parfillskip\@flushglue \lineskip\normallineskip
% \baselineskip\normalbaselineskip
\sloppy
% \let\\\@normalcr
}

% Compatibility with previous versions (temporary!):
\let\fram@d=\MakeFramed \let\endfram@d=\endMakeFramed

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
`

Ecco il sorgente del documento di esempio:
`
\documentclass[12pt,twoside]{book}
\usepackage[T1]{fontenc}
\usepackage[latin1]{inputenc}
%————————————————
\usepackage{titlesec}
% pagestyle
\usepackage{calc}% needed here
\widenhead*{0pt}{\marginparsep + \marginparwidth} % symmetrically
\renewpagestyle{plain}{}
\newpagestyle{latexbook}[\bfseries]{
\headrule
\sethead%
[\thepage][][\chaptertitle]%
{\thesection\ \sectiontitle}{}{\thepage}
\setfoot[][][]{}{}{}
}
\pagestyle{latexbook}
%————————————————
% myframed, providing background color across pagebreaks
%
\usepackage{xcolor,lipsum}
\definecolor{mylightgray}{rgb}{0.95,0.95,0.95}

\usepackage{myframed}

\newlength\sidebarMyBox
\newlength\envruleMyBox
\newlength\envborderMyBox
\setlength\sidebarMyBox{1.5pt}% 1.5mm
\setlength\envruleMyBox{0.4pt}
\setlength\envborderMyBox{2.0mm}% A:2.5

\makeatletter
\long\def\MyBoxfboxs#1{%
\leavevmode
\setbox\@tempboxa\hbox{%
\color@begingroup
\kern\fboxsep{#1}\kern\fboxsep
\color@endgroup}%
\@frames@x\relax}
\def\MyBoxframeboxs{%
\@ifnextchar(%)
\@framepicbox{\@ifnextchar[\@MyBoxframeboxs\MyBoxfboxs}}
\def\@MyBoxframeboxs[#1]{%
\@ifnextchar[%]
{\@iMyBoxframeboxs[#1]}%
{\@iMyBoxframeboxs[#1][c]}}
\long\def\@iMyBoxframeboxs[#1][#2]#3{%
\leavevmode
\@begin@tempboxa\hbox{#3}%
\newlength\tempdima%
\setlength\@tempdima{#1}%
\setbox\@tempboxa\hb@xt@\@tempdima
{\kern\fboxsep\csname bm@#2\endcsname\kern\fboxsep}%
\@frames@x{\kern-\fboxrule}%
\@end@tempboxa}
\def\@frames@x#1{%
\@tempdima\fboxrule
\advance\@tempdima\fboxsep
\advance\@tempdima\dp\@tempboxa
\hbox{%
\lower\@tempdima\hbox{%
\vbox{%
%\hrule\@height\fboxrule
\hbox{%
\vrule\@width\fboxrule
#1%
\vbox{%
\vskip\fboxsep
\box\@tempboxa
\vskip\fboxsep}%
#1%
\vrule\@width\fboxrule}%}%
}%\hrule\@height\fboxrule}%
}%
}%
}

\def\MyBoxfcolorbox#1#{\MyBoxcolor@fbox{#1}}
\def\MyBoxcolor@fbox#1#2#3{%
\color@b@x{\fboxsep\z@\color#1{#2}\MyBoxfboxs}{\color#1{#3}}}
\makeatother

\newenvironment{MyBoxFramed}{%
\def\FrameCommand{\fboxrule=\the\sidebarMyBox \fboxsep=\the\envborderMyBox%
\MyBoxfcolorbox%
{darkgray}% border
{mylightgray}% background
}%
\MakeFramed{\FrameRestore}}%
{\endMakeFramed}
%————————————————
% using ntheorem for starting and closing parts
%
\usepackage{ntheorem}
\usepackage{marvosym}% \Keyboard, \ComputerMouse
\usepackage{pifont} % for dingbats
%—
% definisce uno stile di teorema ad header vuoto
% richiede ntheorem.sty
\makeatletter
\newtheoremstyle{mytheoremstyle}
{\item[]}%
{\item[]}
\makeatother
\theoremstyle{mytheoremstyle}% seleziona lo stile appena definito
%—
% my new theorem-like environment
\newtheorem{MyBoxT}{}[chapter]
%—
% MyBox labels & format
\newcommand\MyBoxLabel{MyBox}
\newcommand\MyBoxLabelFormat{%
\color{white}{\bf\ \MyBoxLabel\ \theMyBoxT}\hfill\raisebox{-2pt}[0pt][0pt]{\Huge\Keyboard\hspace{2pt}\ComputerMouse}\ }
\newcommand\MyBoxEndMark{\color{white}\ding{118}}
%—
% defining the environment
\newenvironment{MyBox}{%
%###########################################################
% ciò che viene eseguito all'inizio dell'environment
%###########################################################
\begin{MyBoxT}
%\stepcounter{myexamplecounter}% incrementa il contatore
\par\vskip 8pt% a capo e skip verticale
\noindent%
% striscia colorata
\hskip -\sidebarMyBox%
%\hskip -\envruleMyBox%
\hskip -\envborderMyBox%
\makebox%
[0em][l]%
{%
{\color{darkgray}\rule[-8pt]%
{\textwidth%
+2\sidebarMyBox%
+2\envborderMyBox%
%+\envborderMyBox%
}{1.8em}}%
}%
% scritta
\makebox%
[1.0\textwidth%
+2\sidebarMyBox%
+2\envborderMyBox%
][l]%
{%
{\MyBoxLabelFormat}
}%
\noindent%
\upshape% testo normale
\par% a capo
\vskip -0.75\baselineskip% skip verticale
}{%
%###########################################################
% ciò che viene eseguito alla fine dell'environment
%###########################################################
%\addvspace{3.2ex plus 0.8ex minus 0.2ex}%
\end{MyBoxT}
\vskip -1.0\baselineskip% skip verticale
\noindent
% striscia colorata
\hskip -\sidebarMyBox%
%\hskip -\envruleMyBox%
\hskip -\envborderMyBox%
\makebox%
[0em][l]%
{%
{\color{darkgray}\rule[-8pt]%
{\textwidth%
+2\sidebarMyBox%
+2\envborderMyBox%
}{1.8em}}%
}%
% simbolo di chiusura
\makebox%
[1.0\textwidth%
+2\sidebarMyBox%
+2\envborderMyBox%
][r]%
{%
{\MyBoxEndMark}\hspace{\sidebarMyBox}%
}%
\par\vskip-2pt%
\noindent
}

\begin{document}

\mainmatter
\chapter{Gnats nel mondo}

\section{Introduzione}

Testo testo testo testo testo testo testo testo testo testo testo testo testo
testo testo testo testo testo testo testo testo testo testo testo testo
testo testo testo testo testo testo testo testo testo testo testo testo
testo testo testo testo testo testo testo testo testo testo testo testo
testo testo testo testo testo testo testo testo testo testo testo testo
testo testo testo testo testo testo testo testo testo testo testo testo
testo testo testo testo testo testo testo testo testo testo testo testo
testo testo testo testo testo testo testo testo testo testo testo testo.

Ecco un esempio di box che entra in una pagina.\\
\begin{MyBox}\label{mat:One}
\begin{MyBoxFramed}
Per il sistema massa-molla-smorzatore di fig.XX si ha quanto segue.\\
\lipsum[2]
\end{MyBoxFramed}
\end{MyBox}

Segue un altro esempio: un box che comprende più di un salto pagina.\\
\begin{MyBox}\label{mat:Two}
\begin{MyBoxFramed}
Per il sistema massa-molla-smorzatore di fig.XX si ha quanto segue.\\
\lipsum
\lipsum
\end{MyBoxFramed}
\end{MyBox}

\end{document}
`

Ho usato il codice di Andrea per gestire il colore di background e le barre verticali ed il pacchetto ntheorem per l’intestazione e la chiusura.

Agostino

Go to top