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.

I want to obtain a text surrounded with begin and end commands, as the following.

\begincommand
Here any text is written.
\endcommand

\surroundedtext % this command is replaced with "Here any text is written."

Of course, the object is accomplished if I define \begincommand so that it takes an argument.

\def\begincommand#1{...}
\begincommand{Here any text is written.}

But, I don't want to use braces. Please let me know if you have the solution.

share|improve this question
add comment

2 Answers

up vote 8 down vote accepted

Assuming Plain TeX, you can define

\long\def\begincommand#1 \endcommand{...}

but this will force you to write

\begincommand
Text to be stored
\endcommand

with \endcommand on a line by itself or

\begincommand
Text to be stored \endcommand

with a space between the last word and \endcommand (but no empty line before \encommand). Alternatively, you can define

\long\def\begincommand#1\endcommand{...}

and then do something about the possible final space when \endcommand is on a line by itself. One should know the context, in order to give better advice.

If you're using LaTeX, this is not good programming style. If what you're looking for is an environment form for a "definition", then the environ package suggested by Leo Liu is handy, but poses some challenges for defining a command that uses the environment's body to be used subsequently.

\usepackage{environ}

\NewEnviron{command}{%
  \long\xdef\surroundedtext{%
    \unexpanded{<pre>}%
    \unexpanded\expandafter{\BODY}%
    \unexpanded{<post>}}%
  }

Thus the code

\begin{command}
Text to be stored
\end{command}

will be equivalent to

\newcommand{\surroundedtext}{<pre>Text to be stored<post>}

Add whatever you need in place of <pre> and <post>

share|improve this answer
    
Thank you. It's very nice solution. –  skymountain Feb 14 '12 at 16:02
1  
@skymountain: I think it's a bad LaTeX-style. Either you work with \begin{}..\end{} or a single command. –  Marco Daniel Feb 14 '12 at 16:36
    
@MarcoDaniel That's why I said "Assuming Plain TeX". –  egreg Feb 14 '12 at 16:39
1  
@egreg: I wasn't able to interpret it in this way ;-) –  Marco Daniel Feb 14 '12 at 18:37
    
@MacroDaniel Hmm... you mean that I should adopt Leo's solution "generally"? –  skymountain Feb 14 '12 at 18:44
show 2 more comments

You can see the implementation of LaTeX package environ, or just use this package.

Example:

\documentclass{article}
\usepackage{environ}
\NewEnviron{foo}{(\textbf{\BODY})}
\begin{document}
\begin{foo}
body
\end{foo}
\end{document}
share|improve this answer
    
Great, thank you! –  skymountain Feb 14 '12 at 15:58
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.