Generalnewcommand with option command

LaTeX specific issues not fitting into one of the other forums of this category.
Post Reply
thomderainette
Posts: 1
Joined: Mon Mar 25, 2013 12:52 pm

newcommand with option command

Post by thomderainette »

Hi all,

I want to create a kind of wrapper of section, subsection, part...

I begin with:

Code: Select all

\newcommand\wrappsection[2]{
	\setcounter{section}{#1}
	%[...]
	\section{#2}
	%[...]
}
\wrappsection{counter-1}{name of section}
And I copy this for each other level.

Can we do something like:

Code: Select all

\newcommand\wrappall[3]{
	\setcounter{#1}{#2}
	%[...]
	\#1{#3} %is ther a way to use #1 as a command
	%[...]
}
\wrappall{levelname}{counter-1}{name of section}
Note that it's not a critical point, just a feature I want.
Thanks to any comments.

Thom

Recommended reading 2024:

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

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

cgnieder
Site Moderator
Posts: 2000
Joined: Sat Apr 16, 2011 7:27 pm

newcommand with option command

Post by cgnieder »

Hi Thom,

Welcome to the LaTeX community!

I must confess I don't get what you want these commands for. Manual setting of a counter somehow dismisses LaTeX's principle of not having to set things by hand. I also have a feeling that this approach is error-prone. But maybe it actually makes sense in the context you're using it...

Anyway:
thomderainette wrote:[...]Can we do something like:

Code: Select all

\newcommand\wrappall[3]{
	\setcounter{#1}{#2}
	%[...]
	\#1{#3} %is ther a way to use #1 as a command
	%[...]
}
\wrappall{levelname}{counter-1}{name of section}
[...]
You're probably looking for \csname ...\endcsname or LaTeX's wrapper \@nameuse{}:

Code: Select all

\newcommand\wrappall[3]{% 
	\setcounter{#1}{#2}%
	\csname#1\endcsname{#3}%
}
or

Code: Select all

% make @ a letter so it can be used in command names:
\makeatletter
\newcommand\wrappall[3]{% 
	\setcounter{#1}{#2}%
	\@nameuse{#1}{#3}%
}
\makeatother
Note that you should end lines with % if you want to avoid spurious spaces in your command definition.

Regards
site moderator & package author
Post Reply