COMPGROUPS.NET | Search | Post Question | Groups | Stream | About | Register

How to insert some commands before an existing environment?

• Follow

For example, I want to insert some commands such as
\centering
and some other commands come from ccaption package before the other
commands in the figure environment,
and no need to use a new environment name.

How to implement this effect?
I've tried the following codes, but it didn't work.

\def\tempfigure\figure
\renewcommand\figure
{\tempfigure
\hangcaption
\captiontitlefont{\sffamily}
\centering}

But, the following codes do work well:
\begin{figure}
\hangcaption
\captiontitlefont{\sffamily}
\centering
.....
\end{figure}

 0
Reply rockyzhz (6) 12/20/2009 7:20:47 PM

In article <1080cf52-5e6f-4b78-9f6f-4d9aa19c7ecf@u18g2000pro.googlegroups.com>,
Rocky Zhang <rockyzhz@gmail.com> writes:
>For example, I want to insert some commands such as
>\centering
>and some other commands come from ccaption package before the other
>commands in the figure environment,
>and no need to use a new environment name.
>
>How to implement this effect?
>I've tried the following codes, but it didn't work.
>
>\def\tempfigure\figure

i presume you mean

\def\tempfigure{\figure}

all (other) bets are off if you _didn't_ do that -- i can't imagine
what the diagnostics might be like.

>\renewcommand\figure
> {\tempfigure
>  \hangcaption
>  \captiontitlefont{\sffamily}
>  \centering}

\tempfigure is "just" \figure, so the first thing \figure does is to
call itself.  hence the diagnostic.

\let\tempfigure\figure

makes a "frozen" copy of \figure, which isn't changed by subsequent
redefinition, so use that instead of the (corrected) \def.

(note the confusing fact that \let doesn't want braces round its
second argument, like \def does.  this is the sort of thing you get,
dabbling in tex internals (which i presume you don't often do, from

>But, the following codes do work well:
>\begin{figure}
>  \hangcaption
>  \captiontitlefont{\sffamily}
>  \centering
>....
>\end{figure}

of course.
--
Robin Fairbairns, Cambridge

 0

rf10@cl.cam.ac.uk (Robin Fairbairns) writes:

> In article
> Rocky Zhang <rockyzhz@gmail.com> writes:
>>For example, I want to insert some commands such as
>>\centering
>>and some other commands come from ccaption package before the other
>>commands in the figure environment,
>>and no need to use a new environment name.
>>
>>How to implement this effect?
>>I've tried the following codes, but it didn't work.
>>
>>\def\tempfigure\figure
>
> i presume you mean
>
> \def\tempfigure{\figure}
>
> all (other) bets are off if you _didn't_ do that -- i can't imagine
> what the diagnostics might be like.

\tempfigure would be a macro that has to be called like
\tempfigure\figure\renewcommand\figure. Its expansion text is
\tempfigure\hangcaption\captiontitlefont{\sffamily} \centering. \figure
is never changed, and as long as \tempfigure doesn't appear in the
document, there is no difference at all, and no diagnostic messages.

\documentclass{article}
\def\tempfigure\figure
\renewcommand\figure
{\tempfigure
\hangcaption
\captiontitlefont{\sffamily}
\centering}
\begin{document}
\ttfamily
\meaning\figure\par
\meaning\tempfigure
\end{document}

--
Change “LookInSig” to “tcalveu” to answer by mail.

 0

> i presume you mean
>
> \def\tempfigure{\figure}
>
> all (other) bets are off if you _didn't_ do that -- i can't imagine
> what the diagnostics might be like.
>
> \tempfigure is "just" \figure, so the first thing \figure does is to
> call itself. =A0hence the diagnostic.
>
> \let\tempfigure\figure

yeah, I changed the \def command into \let, that codes go well.
But there is still a problem that how to pass the PLACEMENT parameter
to the redefined figure environment?

I've tried some codes with PLACEMENT parameter, and then the figure(or
table) appeared at the end of document.

> makes a "frozen" copy of \figure, which isn't changed by subsequent
> redefinition, so use that instead of the (corrected) \def.
>
> (note the confusing fact that \let doesn't want braces round its
> second argument, like \def does. =A0this is the sort of thing you get,
> dabbling in tex internals (which i presume you don't often do, from
>
>
> of course.
> --
> Robin Fairbairns, Cambridge


 0

Rocky Zhang <rockyzhz@gmail.com> wrote:

> > i presume you mean
> >
> > \def\tempfigure{\figure}
> >
> > all (other) bets are off if you _didn't_ do that -- i can't imagine
> > what the diagnostics might be like.
> >
> > \tempfigure is "just" \figure, so the first thing \figure does is to
> > call itself. �hence the diagnostic.
> >
> > \let\tempfigure\figure
>
> yeah, I changed the \def command into \let, that codes go well.
> But there is still a problem that how to pass the PLACEMENT parameter
> to the redefined figure environment?
>
> I've tried some codes with PLACEMENT parameter, and then the figure(or
> table) appeared at the end of document.

\makeatletter
\hangcaption
\captiontitlefont{\sffamily}
\centering}
\makeatother

The macro \@floatboxreset is the last thing which is executed
before examining the floating environment's content. With that
code we supplement the definition of \@floatboxreset with the
desired commands.

But if you want every caption in that style, why not simply putting
the commands

\hangcaption
\captiontitlefont{\sffamily}

into the preamble?

Ciao
Enrico

 0

On 12=E6=9C=8821=E6=97=A5, =E4=B8=8A=E5=8D=885=E6=97=B635=E5=88=86, Enrico =
Gregorio <grego...@math.unipd.it> wrote:
> Rocky Zhang <rocky...@gmail.com> wrote:
> > > i presume you mean
>
> > > \def\tempfigure{\figure}
>
> > > all (other) bets are off if you _didn't_ do that -- i can't imagine
> > > what the diagnostics might be like.
>
> > > \tempfigure is "just" \figure, so the first thing \figure does is to
> > > call itself. =C3=8Ahence the diagnostic.
>
> > > \let\tempfigure\figure
>
> > yeah, I changed the \def command into \let, that codes go well.
> > But there is still a problem that how to pass the PLACEMENT parameter
> > to the redefined figure environment?
>
> > I've tried some codes with PLACEMENT parameter, and then the figure(or
> > table) appeared at the end of document.
>
> \makeatletter
> =C2=A0 \hangcaption
> =C2=A0 \captiontitlefont{\sffamily}
> =C2=A0 \centering}
> \makeatother
>
> The macro \@floatboxreset is the last thing which is executed
> before examining the floating environment's content. With that
> code we supplement the definition of \@floatboxreset with the
> desired commands.
>
> But if you want every caption in that style, why not simply putting
> the commands
>
> \hangcaption
> \captiontitlefont{\sffamily}
>
> into the preamble?
Because I want to display the figure and table in different style.
>
> Ciao
>


 0

Rocky Zhang <rockyzhz@gmail.com> wrote:

> On 1221, 535p, Enrico Gregorio <grego...@math.unipd.it> wrote:
> > Rocky Zhang <rocky...@gmail.com> wrote:
> > > > i presume you mean
> >
> > > > \def\tempfigure{\figure}
> >
> > > > all (other) bets are off if you _didn't_ do that -- i can't imagine
> > > > what the diagnostics might be like.
> >
> > > > \tempfigure is "just" \figure, so the first thing \figure does is to
> > > > call itself. �hence the diagnostic.
> >
> > > > \let\tempfigure\figure
> >
> > > yeah, I changed the \def command into \let, that codes go well.
> > > But there is still a problem that how to pass the PLACEMENT parameter
> > > to the redefined figure environment?
> >
> > > I've tried some codes with PLACEMENT parameter, and then the figure(or
> > > table) appeared at the end of document.
> >
> > \makeatletter
> > � \hangcaption
> > � \captiontitlefont{\sffamily}
> > � \centering}
> > \makeatother
> >
> > The macro \@floatboxreset is the last thing which is executed
> > before examining the floating environment's content. With that
> > code we supplement the definition of \@floatboxreset with the
> > desired commands.
> >
> > But if you want every caption in that style, why not simply putting
> > the commands
> >
> > \hangcaption
> > \captiontitlefont{\sffamily}
> >
> > into the preamble?
> Because I want to display the figure and table in different style.

Use the caption package instead of ccaption.

Ciao
Enrico

 0

On 12=E6=9C=8821=E6=97=A5, =E4=B8=8A=E5=8D=885=E6=97=B635=E5=88=86, Enrico =
Gregorio <grego...@math.unipd.it> wrote:
> Rocky Zhang <rocky...@gmail.com> wrote:
> > > i presume you mean
>
> > > \def\tempfigure{\figure}
>
> > > all (other) bets are off if you _didn't_ do that -- i can't imagine
> > > what the diagnostics might be like.
>
> > > \tempfigure is "just" \figure, so the first thing \figure does is to
> > > call itself. =C3=8Ahence the diagnostic.
>
> > > \let\tempfigure\figure
>
> > yeah, I changed the \def command into \let, that codes go well.
> > But there is still a problem that how to pass the PLACEMENT parameter
> > to the redefined figure environment?
>
> > I've tried some codes with PLACEMENT parameter, and then the figure(or
> > table) appeared at the end of document.
>
> \makeatletter
> =C2=A0 \hangcaption
> =C2=A0 \captiontitlefont{\sffamily}
> =C2=A0 \centering}
> \makeatother
>
> The macro \@floatboxreset is the last thing which is executed
> before examining the floating environment's content. With that
> code we supplement the definition of \@floatboxreset with the
> desired commands.
I've search the definition of \g@addto@macro, and then find it in the
"source2e".
every float environment?
> But if you want every caption in that style, why not simply putting
> the commands
>
> \hangcaption
> \captiontitlefont{\sffamily}
>
> into the preamble?
>
> Ciao
>


 0

Rocky Zhang <rockyzhz@gmail.com> wrote:

> On 1221, 535p, Enrico Gregorio <grego...@math.unipd.it> wrote:
> > Rocky Zhang <rocky...@gmail.com> wrote:
> > > > i presume you mean
> >
> > > > \def\tempfigure{\figure}
> >
> > > > all (other) bets are off if you _didn't_ do that -- i can't imagine
> > > > what the diagnostics might be like.
> >
> > > > \tempfigure is "just" \figure, so the first thing \figure does is to
> > > > call itself. �hence the diagnostic.
> >
> > > > \let\tempfigure\figure
> >
> > > yeah, I changed the \def command into \let, that codes go well.
> > > But there is still a problem that how to pass the PLACEMENT parameter
> > > to the redefined figure environment?
> >
> > > I've tried some codes with PLACEMENT parameter, and then the figure(or
> > > table) appeared at the end of document.
> >
> > \makeatletter
> > � \hangcaption
> > � \captiontitlefont{\sffamily}
> > � \centering}
> > \makeatother
> >
> > The macro \@floatboxreset is the last thing which is executed
> > before examining the floating environment's content. With that
> > code we supplement the definition of \@floatboxreset with the
> > desired commands.
> I've search the definition of \g@addto@macro, and then find it in the
> "source2e".
> every float environment?

Yes.

Ciao
Enrico

 0

Robin Fairbairns wrote:
> \let\tempfigure\figure
>
> makes a "frozen" copy of \figure, which isn't changed by subsequent
> redefinition, so use that instead of the (corrected) \def.
>
> (note the confusing fact that \let doesn't want braces round its
> second argument, like \def does.  this is the sort of thing you get,
> dabbling in tex internals (which i presume you don't often do, from

On the other hands, \let allows you to use an equal sign. It's not
necessary, but I think it makes the code much more readable:

\let\tempfigure=\figure

--
You should make a point of trying every experience once -- except
incest and folk-dancing.
-- A. Bax, "Farewell My Youth"

Eduardo M KALINOWSKI
eduardo@kalinowski.com.br

 0

On 12=D4=C221=C8=D5, =C9=CF=CE=E74=CA=B124=B7=D6, r...@cl.cam.ac.uk (Robin =
Fairbairns) wrote:
..com>,
>  Rocky Zhang <rocky...@gmail.com> writes:
>
> >For example, I want to insert some commands such as
> >\centering
> >and some other commands come from ccaption package before the other
> >commands in the figure environment,
> >and no need to use a new environment name.
>
> >How to implement this effect?
> >I've tried the following codes, but it didn't work.
>
> >\def\tempfigure\figure
>
> i presume you mean
>
> \def\tempfigure{\figure}
>
> all (other) bets are off if you _didn't_ do that -- i can't imagine
> what the diagnostics might be like.
>
> >\renewcommand\figure
> > {\tempfigure
> >  \hangcaption
> >  \captiontitlefont{\sffamily}
> >  \centering}
>
> \tempfigure is "just" \figure, so the first thing \figure does is to
> call itself.  hence the diagnostic.
>
> \let\tempfigure\figure
>
> makes a "frozen" copy of \figure, which isn't changed by subsequent
> redefinition, so use that instead of the (corrected) \def.
>
> (note the confusing fact that \let doesn't want braces round its
> second argument, like \def does.  this is the sort of thing you get,
> dabbling in tex internals (which i presume you don't often do, from
>
> >But, the following codes do work well:
> >\begin{figure}
> >  \hangcaption
> >  \captiontitlefont{\sffamily}
> >  \centering
> >....
> >\end{figure}
>
> of course.
> --
> Robin Fairbairns, Cambridge

Many thanks to everyone, I've got the particular solutions for figure
and table environments
as the following:

\makeatletter
\renewenvironment{figure}[2][\@empty]
{\ifx\@empty#1\@float{figure}\else\@float{figure}[#1]\fi
\hangcaption
\captiontitlefont{\sffamily}
\centering
#2
}
{\end@float}
\renewenvironment{table}[2][]
{\ifx\@empty#1\@float{table}\else\@float{table}[#1]\fi
\captionnamefont{\hfill}
\captiondelim{\hfill}
\captionstyle{\\}
\captiontitlefont{\sffamily}
\centering
#2
}
{\end@float}
\makeatother

Thanks again.

 0

Rocky Zhang <rockyzhz@gmail.com> wrote:

> Many thanks to everyone, I've got the particular solutions for figure
> and table environments
> as the following:
>
> \makeatletter
> \renewenvironment{figure}[2][\@empty]
>  {\ifx\@empty#1\@float{figure}\else\@float{figure}[#1]\fi

This test doesn't work: if you write \begin{figure}[], it fails.
The default placement for the figure environment is in the macro
\fps@figure (there is \fps@table for "table"); therefore something like

\renewenvironment{figure}[1][\fps@figure]
{\edef\@temp{\noexpand\@float{figure}[#1]}\@temp
...}
{...}

would be more robust (unless you are using babel for French).
I would /never/ add an argument to such environment: this makes
the code not portable to other classes. I don't see any benefit
from writing

\begin{figure}[tp]{\small}

\begin{figure}[tp]
\small

(I guess that your idea is that the second argument contains
declarations for the current figure or table environment).

Ciao
Enrico

 0

On 12=D4=C222=C8=D5, =CF=C2=CE=E710=CA=B126=B7=D6, Enrico Gregorio <grego..=
..@math.unipd.it> wrote:
> Rocky Zhang <rocky...@gmail.com> wrote:
> > Many thanks to everyone, I've got the particular solutions for figure
> > and table environments
> > as the following:
>
> > \makeatletter
> > \renewenvironment{figure}[2][\@empty]
> >  {\ifx\@empty#1\@float{figure}\else\@float{figure}[#1]\fi
>

> This test doesn't work: if you write \begin{figure}[], it fails.
> The default placement for the figure environment is in the macro
> \fps@figure (there is \fps@table for "table"); therefore something like
Oh, I see. In fact, I've tried the command \fps@TYPE, and it didn't
work. thanks:-)
>
> \renewenvironment{figure}[1][\fps@figure]
>   {\edef\@temp{\noexpand\@float{figure}[#1]}\@temp

Here why not use \@float{figure}[#1] directly? Could you elaborate the
difference between them?

>    ...}
>   {...}
>
> would be more robust (unless you are using babel for French).
> I would /never/ add an argument to such environment: this makes
> the code not portable to other classes. I don't see any benefit
> from writing
>
> \begin{figure}[tp]{\small}
>
>
> \begin{figure}[tp]
> \small
immediately. But it seems that the latter form is also compatible with
my original redefinition.
>
> (I guess that your idea is that the second argument contains
> declarations for the current figure or table environment).
>
> Ciao
> Enrico


 0

On 12=D4=C223=C8=D5, =C9=CF=CE=E712=CA=B134=B7=D6, Rocky Zhang <rocky...@gm=
ail.com> wrote:
> On 12=D4=C222=C8=D5, =CF=C2=CE=E710=CA=B126=B7=D6, Enrico Gregorio <grego=
....@math.unipd.it> wrote:
>
> > Rocky Zhang <rocky...@gmail.com> wrote:
> > > Many thanks to everyone, I've got the particular solutions for figure
> > > and table environments
> > > as the following:
>
> > > \makeatletter
> > > \renewenvironment{figure}[2][\@empty]
> > >  {\ifx\@empty#1\@float{figure}\else\@float{figure}[#1]\fi
>
> Thank you for your patient!
Sorry ,                     ~~~~~I mean patience.
>
> > This test doesn't work: if you write \begin{figure}[], it fails.
> > The default placement for the figure environment is in the macro
> > \fps@figure (there is \fps@table for "table"); therefore something like
>
> Oh, I see. In fact, I've tried the command \fps@TYPE, and it didn't
> work. thanks:-)
>
>
>
> > \renewenvironment{figure}[1][\fps@figure]
> >   {\edef\@temp{\noexpand\@float{figure}[#1]}\@temp
>
> Here why not use \@float{figure}[#1] directly? Could you elaborate the
> difference between them?
>
> >    ...}
> >   {...}
>
> > would be more robust (unless you are using babel for French).
> > I would /never/ add an argument to such environment: this makes
> > the code not portable to other classes. I don't see any benefit
> > from writing
>
> > \begin{figure}[tp]{\small}
>
>
> > \begin{figure}[tp]
> > \small
>
> immediately. But it seems that the latter form is also compatible with
> my original redefinition.
>
>
>
>
>
> > (I guess that your idea is that the second argument contains
> > declarations for the current figure or table environment).
>
> > Ciao
> > Enrico- =D2=FE=B2=D8=B1=BB=D2=FD=D3=C3=CE=C4=D7=D6 -
>
> - =CF=D4=CA=BE=D2=FD=D3=C3=B5=C4=CE=C4=D7=D6 -


 0

Rocky Zhang <rockyzhz@gmail.com> wrote:

> On 12��22��, ���10�26�, Enrico Gregorio <grego...@math.unipd.it> wrote:
> > Rocky Zhang <rocky...@gmail.com> wrote:
> > > Many thanks to everyone, I've got the particular solutions for figure
> > > and table environments
> > > as the following:
> >
> > > \makeatletter
> > > \renewenvironment{figure}[2][\@empty]
> > >  {\ifx\@empty#1\@float{figure}\else\@float{figure}[#1]\fi
> >
>
> Thank you for your patient!
>
> > This test doesn't work: if you write \begin{figure}[], it fails.
> > The default placement for the figure environment is in the macro
> > \fps@figure (there is \fps@table for "table"); therefore something like
> Oh, I see. In fact, I've tried the command \fps@TYPE, and it didn't
> work. thanks:-)
> >
> > \renewenvironment{figure}[1][\fps@figure]
> >   {\edef\@temp{\noexpand\@float{figure}[#1]}\@temp
>
> Here why not use \@float{figure}[#1] directly? Could you elaborate the
> difference between them?

Because the expansion of \@float needs to look at the actual characters
that form the optional argument, doing a "loop" on them.

With this \edef, TeX is presented with the token list

\@float{figure}[<expansion of #1>]

Assuming  \def\fps@figure{htp}, a call such as

\begin{figure}

would give

\@float{figure}[htp]

In the case the optional argument is specified, say

\begin{figure}[tp]

the \edef would really do nothing, but would present TeX the
list \@float{figure}[tp].

Actually this doesn't take care for an empty optional argument
either, as it should. :(

So I suggest you something else, again with \@floatboxreset:

\makeatletter
\def\zh@figure{figure}
\def\zh@table{table}
\ifx\@currenvir\zh@figure
\hangcaption
\fi
\ifx\@currenvir\zh@table
\captionnamefont{\hfill}%
\captiondelim{\hfill}%
\captionstyle{\\}%
\fi
\captiontitlefont{\sffamily}
\centering
}
\makeatother

The code in \@floatboxreset is executed at each call of \@float,
but with the conditional inside the code we can distinguish whether
the environment is figure or table and act differently: the macro
\@currenvir contains the current environment's name. Of course you
are not be calling \figure or \table directly, are you? :)

OTOH, with the caption package it's very easy to specify different
setting for figure and table captions:

\captionsetup[figure]{<setup>}
\captionsetup[table]{<setup>}

Ciao
Enrico

 0

On 12=E6=9C=8823=E6=97=A5, =E4=B8=8A=E5=8D=881=E6=97=B609=E5=88=86, Enrico =
Gregorio <grego...@math.unipd.it> wrote:
> Rocky Zhang <rocky...@gmail.com> wrote:
> > On 12=C3=AF=C3=A522=C3=A9=C3=8D, =C3=AC=C3=A5=C3=AB 10=C3=A6=C2=B126=C3=
=A1=E2=80=A6, Enrico Gregorio <grego...@math.unipd.it> wrote:
> > > Rocky Zhang <rocky...@gmail.com> wrote:
> > > > Many thanks to everyone, I've got the particular solutions for figu=
re
> > > > and table environments
> > > > as the following:
>
> > > > \makeatletter
> > > > \renewenvironment{figure}[2][\@empty]
> > > > =C2=A0{\ifx\@empty#1\@float{figure}\else\@float{figure}[#1]\fi
>
> > Thank you for your patient!
>
> > > This test doesn't work: if you write \begin{figure}[], it fails.
> > > The default placement for the figure environment is in the macro
> > > \fps@figure (there is \fps@table for "table"); therefore something li=
ke
> > Oh, I see. In fact, I've tried the command \fps@TYPE, and it didn't
> > work. thanks:-)
>
> > > \renewenvironment{figure}[1][\fps@figure]
> > > =C2=A0 {\edef\@temp{\noexpand\@float{figure}[#1]}\@temp
>
> > Here why not use \@float{figure}[#1] directly? Could you elaborate the
> > difference between them?
>
> Because the expansion of \@float needs to look at the actual characters
> that form the optional argument, doing a "loop" on them.
>
> With this \edef, TeX is presented with the token list
>
> \@float{figure}[<expansion of #1>]
>
> Assuming =C2=A0\def\fps@figure{htp}, a call such as
>
> \begin{figure}
>
> would give
>
> \@float{figure}[htp]
>
> In the case the optional argument is specified, say
>
> \begin{figure}[tp]
>
> the \edef would really do nothing, but would present TeX the
> list \@float{figure}[tp].
>
> Actually this doesn't take care for an empty optional argument
> either, as it should. :(
>
> So I suggest you something else, again with \@floatboxreset:
>
> \makeatletter
> \def\zh@figure{figure}
> \def\zh@table{table}
> =C2=A0 \ifx\@currenvir\zh@figure
> =C2=A0 =C2=A0 \hangcaption
> =C2=A0 \fi
> =C2=A0 \ifx\@currenvir\zh@table
> =C2=A0 =C2=A0 \captionnamefont{\hfill}%
> =C2=A0 =C2=A0 \captiondelim{\hfill}%
> =C2=A0 =C2=A0 \captionstyle{\\}%
> =C2=A0 \fi
> =C2=A0 \captiontitlefont{\sffamily}
> =C2=A0 \centering
> =C2=A0}
> \makeatother
>
> The code in \@floatboxreset is executed at each call of \@float,
> but with the conditional inside the code we can distinguish whether
> the environment is figure or table and act differently: the macro
> \@currenvir contains the current environment's name. Of course you
> are not be calling \figure or \table directly, are you? :)
>
> OTOH, with the caption package it's very easy to specify different
> setting for figure and table captions:
>
> \captionsetup[figure]{<setup>}
> \captionsetup[table]{<setup>}
>
> Ciao
>

Thank you for your wanderful explain! :-)

 0

15 Replies
184 Views

3/30/2013 2:19:28 AM