# Can a command begin and end within {} and {}?

Is there any way to have a command, such as \section{}, begin and end inside two other commands?

\documentclass{article}
\newcommand{\thisisaspecialcommand}[1]{
#1
}
\begin{document}
\thisisaspecialcommand{\section{} % The section begins here.
\thisisaspecialcommand{This is the section's title.}
\thisisaspecialcommand{}} % The section ends here.
(text)
\end{document}


In this sample code, LaTeX does not understand which { begins the section and which } ends the section.

-
This question is fairly ambiguous, since \section{} clearly provides an empty argument to \section and typesets correctly. What is it you're after exactly in terms of the typeset output? –  Werner Nov 9 '11 at 0:33
It would help if you were clearer about your purpose. –  egreg Nov 9 '11 at 0:33
I hope the output will be the same as: \section{This is the section's title}, not an empty section. I am using a script which generates LaTeX code, unfortunately, sometimes it puts \section{ and } in different places, so I wonder if there is a way to let the command bridge these places? –  Village Nov 9 '11 at 0:51
No, it will not be the same as \section{This is ...}. In your code, \this... first expands to \section{}\this...{...}\this{} (sort of). And then it further expands, \section{} expands to a heading with empty title the number, and as part of the expansion of \section, it starts a new paragraph, which will typeset \this...{...}. After this typeset, the last \this... is expanded, which is a space. I don't get what you are after exactly. \begingroup ? Are you saying you want everything between \section{ and } becomes the title? –  Yan Zhou Nov 9 '11 at 1:30

Maybe you're searching for a kind of environment that uses the body of the environment as the argument of a command. In this case, the package environ may help you:

\documentclass{article}
\usepackage{environ}

\NewEnviron{specialenvironment}{\section{\BODY}}
\begin{document}
\begin{specialenvironment}
\end{specialenvironment}
\end{document}

-
Good, that seems to solve it. Thank you! –  Village Nov 9 '11 at 12:51

I agree with all the above comments that this does not really make sense, but for what its worth, the code below does seem to achieve the goal stated in the comments:

output will be the same as: \section{This is the section's title}, not an empty section

One change that this requires is that there be a different command inside and outside. So below I have used thisisaDifferentSpecialcommand as the new command that is to be used within the original thisisaspecialcommand. Since you want this output to be identical to a regular \section{This is the section's title.} command, I added it at the beginning so that we can compare the two outputs. The following MWE yields:

\documentclass{article}

\let\OldSection\section% Save definition of \section
\newcommand{\thisisaspecialcommand}[1]{%
\renewcommand{\section}[1]{}% Disable \section within \thisisaspecialcommand