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.

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

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.

share|improve this question
    
Could be done with \def, but it looks like you want to define an environment, not a command. – jon Jan 25 at 16:29
up vote 3 down vote accepted

A higher level possibility is with xparse:

\usepackage{xparse}

\NewDocumentCommand{\teststart}{+u{\testend}}{%
  do something with #1%
}

The + allows the argument to contain blank lines.

share|improve this answer
    
\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 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

\documentclass{article}

\def\teststart#1\testend{left-#1-right} 
\begin{document}

\teststart this is something \testend

\end{document}

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

\documentclass{article}

\long\def\teststart#1\testend{left-#1-right} 
\begin{document}

\teststart this is

something \testend

\end{document}
share|improve this answer

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.

\usepackage{environ}
\NewEnviron{test}{<before>\dosomethingtobody{\BODY}<after>}
share|improve this answer
    
The \BODY macro is very helpful. Do you know if environ package accepts more than two optional arguments? – ehsan Feb 2 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? – Andrew Cashner Feb 2 at 16:25

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

\documentclass{article}
\newcommand\teststart{%
  \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
}
\begin{document}
Here is some text, \teststart which 
has test content\testend within it.

And that content \teststart can go over multiple paragraphs.

For example.\testend
\end{document}

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).

share|improve this answer
    
Nice, but it does not allow manipulating the argument, because there's none. – egreg Jan 25 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. – Norman Gray Jan 25 at 22:31

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.