Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

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.

share|improve this question
1  
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
2  
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
add comment

2 Answers

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:

\documentclass{article}
\usepackage{environ}

\NewEnviron{specialenvironment}{\section{\BODY}}
\begin{document}
\begin{specialenvironment}
Section heading
\end{specialenvironment}
\end{document}
share|improve this answer
    
Good, that seems to solve it. Thank you! –  Village Nov 9 '11 at 12:51
add comment

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

\documentclass{article}

\let\OldSection\section% Save definition of \section
\newcommand*{\thisisaDifferentSpecialcommand}[1]{#1}%
\newcommand{\thisisaspecialcommand}[1]{%
    \renewcommand{\section}[1]{}% Disable \section within \thisisaspecialcommand
    \OldSection{#1}% Apply section header
}
\begin{document}
    \section{This is the section's title.}
    (text)

    \thisisaspecialcommand{\section{}% The section begins here.
    \thisisaDifferentSpecialcommand{This is the section's title.}
    \thisisaDifferentSpecialcommand{}}% The section ends here.
    (text)
\end{document}
share|improve this answer
add comment

Your Answer

 
discard

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.