I know that in LaTeX newcommand is defined as:

\newcommand{name}[num] {definition }

But I want to define a new command whose argument is not defined between brackets, and it is defined with between \begin and \end commands. For example, something like this:

\teststart bla-bla-bla \testend

Maybe I should define two newcommands, one for \teststart and one for \testend.

I should mention that I have seen such commands in the changebar package, \cbstart and \cbend.

  • Could be done with \def, but it looks like you want to define an environment, not a command.
    – jon
    Jan 25, 2016 at 16:29

4 Answers 4


A higher level possibility is with xparse:


  do something with #1%

The + allows the argument to contain blank lines.

  • \def worked for my case but I prefer \NewDocumentCommand and xparse package. This is exactly what I want, specially xparse package lets me define more than one optional argument.
    – ehsan
    Jan 25, 2016 at 17:51

This is possible using TeX's \def, where you require a specific argument text. The argument text includes both the specific sequence to be replaced, as well as the arguments in the form #X (where X is a number from 1 through 9):

enter image description here



\teststart this is something \testend


If you want to allow for paragraph breaks between \teststart and \testend, then you need to make the definition using \long

enter image description here



\teststart this is

something \testend


You can define an environment this way:

\newenvironment{test}{<commands to execute at beginning}{<commands for end}

You use the environment like this:

\begin{test} bla-bla-bla \end{test}

If you want to apply a command to the contents of the environment as a whole, you can do this with the environ package. The macro \BODY stands for the whole body of the environment, so you can apply a command to it.

  • The \BODY macro is very helpful. Do you know if environ package accepts more than two optional arguments?
    – ehsan
    Feb 2, 2016 at 10:50
  • The documentation demonstrates environments that take arguments, both required and optional. But the only way to have more than one optional argument is to use xparse, I think. Perhaps this should be a new question? Feb 2, 2016 at 16:25

And for contrast, a rather more low-level approach is as follows:

  \begingroup           % start a group
  \bfseries             % make the content bold, for demo purposes
  \let\testend\endgroup % within this 'environment' (only),
                        % \testend acts to close the group
Here is some text, \teststart which 
has test content\testend within it.

And that content \teststart can go over multiple paragraphs.

For example.\testend

This is broadly similar to the way that the \begin{env}...\end{env} mechanism works.

The commands \begingroup and \endgroup create and terminate a group without using {...} (which could not, of course, appear unbalanced within a definition).

  • Nice, but it does not allow manipulating the argument, because there's none.
    – egreg
    Jan 25, 2016 at 21:18
  • @egreg True, but \newcommand\teststart[1]{\begingroup \bfseries Test #1: \let\testend\endgroup} was going to be an exercise for the reader... Was being able to manipulate the text between \teststart ...\testend a requirement? Hmm: maybe so. In that case, @Werner's or your solutions would indeed be necessary. Jan 25, 2016 at 22:31

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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