GeneralIs there a "providecommand"-like cmd for counter & length?

LaTeX specific issues not fitting into one of the other forums of this category.
Post Reply
yoyoimut
Posts: 120
Joined: Mon Oct 19, 2009 6:58 am

Is there a "providecommand"-like cmd for counter & length?

Post by yoyoimut »

Is there a command that behaves like "\providecommand" but to define a counter or length ?


thank you in advance.


regards,

Yuko

Recommended reading 2024:

LaTeXguide.org • LaTeX-Cookbook.net • TikZ.org

NEW: TikZ book now 40% off at Amazon.com for a short time.

josephwright
Site Moderator
Posts: 814
Joined: Tue Jul 01, 2008 2:19 pm

Is there a "providecommand"-like cmd for counter & length?

Post by josephwright »

Not in the kernel: you want something like:

Code: Select all

\makeatletter
\newcommand*\providecounter[1]{%
  \@ifundefined{c@#1}%
    {\@definecounter{#1}}%
    {}%
}
\makeatother
Joseph Wright
yoyoimut
Posts: 120
Joined: Mon Oct 19, 2009 6:58 am

Re: Is there a "providecommand"-like cmd for counter & lengt

Post by yoyoimut »

Thanks Joseph Wright,

The counter works well. :P

I tried to modify your code for the length but it did not work.


Thank you in advance.


regards,

Yuko.
josephwright
Site Moderator
Posts: 814
Joined: Tue Jul 01, 2008 2:19 pm

Is there a "providecommand"-like cmd for counter & length?

Post by josephwright »

Well, LaTeX lengths are just TeX skips, so

Code: Select all

\makeatletter
\newcommand*\providelength[1]{%
  \begingroup
    \escapechar\m@ne
    \xdef\@gtempa{\string#1}%
  \endgroup
  \@ifundefined{\@gtempa}%
    {\newskip#1}%
    {}%
}
\makeatother
The extra code here is needed as \@ifundefined needs the name of the new length (for example "mylength") and not the token ("\mylength"). See the same approach in \providecommand.
Joseph Wright
yoyoimut
Posts: 120
Joined: Mon Oct 19, 2009 6:58 am

Re: Is there a "providecommand"-like cmd for counter & lengt

Post by yoyoimut »

Thank Joseph Wright,

The code works well. ;)

I am sorry, actually I have one more :P

I need \providesavebox associated with \newsavebox.

Should I create a new topic for this question?


Thank you in advance.


regards,

Yuko.
Post Reply