TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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

    \thisisaspecialcommand{\section{} % The section begins here.
    \thisisaspecialcommand{This is the section's title.}
    \thisisaspecialcommand{}} % The section ends here.

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

share|improve this question
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
up vote 7 down vote accepted

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:


Section heading
share|improve this answer
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:

enter image description here


\let\OldSection\section% Save definition of \section
    \renewcommand{\section}[1]{}% Disable \section within \thisisaspecialcommand
    \OldSection{#1}% Apply section header
    \section{This is the section's title.}

    \thisisaspecialcommand{\section{}% The section begins here.
    \thisisaDifferentSpecialcommand{This is the section's title.}
    \thisisaDifferentSpecialcommand{}}% The section ends here.
share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.