Difference between revisions of "Command/define"

From Wiki
Jump to navigation Jump to search
m (Text replace - "\[\[cmd:(.*)\|(.*)\]\]" to "{{cmd|$1}}")
m (Add category; improve example)
Line 21: Line 21:
  
 
== Description ==  
 
== Description ==  
<code>\define</code> is like the TeX primitive <code>\def</code>: it defines a new TeX macro.  
+
<code>\define</code> is like the TeX primitive <code>\def</code>: it defines a new TeX macro. Unlike <code>\def</code>, '''<code>\define</code> can never redefine''' an already defined command.
  
=== define vs def ===
+
The optional bracketed argument denotes the number of parameters you can give  
Unlike <code>\def</code>, it will '''not allow you to redefine''' an already defined command.
 
 
 
The optional (bracketed) argument denotes the number of parameters you can give  
 
 
to your command. "text" is the result when the macro gets expanded.  
 
to your command. "text" is the result when the macro gets expanded.  
  
== pitfalls ==
+
== Pitfalls ==
<code>\define</code> does not use <code>\long</code>, that means in MkII
+
<code>\define</code> does not use {{cmd|long}}, that means in MkII
multiple paragraphs (e.g. through <code>\par</code> or <code>\endgraf</code>)
+
multiple paragraphs (e.g. through {{cmd|par}} or {{cmd|endgraf}})
 
inside arguments will not work.
 
inside arguments will not work.
  
 
The name of the new command may not include numbers.
 
The name of the new command may not include numbers.
 
 
  
 
== Example ==
 
== Example ==
  
<context source="yes">
+
<context mode=mkiv source="yes">
\define[1]\hello{Good morning, #1!}
+
\define\hello{Good morning}
  
 
% You may not use numbers
 
% You may not use numbers
% \define[1]\mp3{The ID3 Tag of the MP3 file is, #1!}
+
% \define[1]\mp3{The ID3 Tag of the MP3 file is: #1}
 
% but this will work:
 
% but this will work:
\define[1]\mpThree{The ID3 Tag of the MP3 file is, #1!}
+
\define[1]\mpThree{The ID3 Tag of the MP3 file is: #1}
  
% You may not use umlauts in mkii, but in mkiv  
+
% You can use non-ASCII characters in MkIV, but not in MkII.
% \define[1]\KüFst{Küstenfunkstelle, #1!}
+
% This example is compiled using mkiv.
 +
\define[1]\KüFst{Küstenfunkstelle, #1!}
  
 
\starttext
 
\starttext
\hello{mom}
+
\hello
 +
 
 +
\KüFst{12}
 +
 
 
\stoptext
 
\stoptext
 
</context>
 
</context>
  
 
== See also ==
 
== See also ==
[http://en.wikibooks.org/wiki/TeX/def http://en.wikibooks.org/wiki/TeX/def]
 
  
<!-- something like {{cmd|goto}} -->
+
* [http://en.wikibooks.org/wiki/TeX/def http://en.wikibooks.org/wiki/TeX/def]
  
 
== Help from ConTeXt-Mailinglist/Forum ==
 
== Help from ConTeXt-Mailinglist/Forum ==
Line 65: Line 63:
 
{{Forum|{{SUBPAGENAME}}}}
 
{{Forum|{{SUBPAGENAME}}}}
  
[[Category:Commands|define]]
+
[[Category:Command/Internals|define]]

Revision as of 10:37, 22 May 2012

\define

Syntax

\define[...]\command{...}
[...] number
{...} text


Description

\define is like the TeX primitive \def: it defines a new TeX macro. Unlike \def, \define can never redefine an already defined command.

The optional bracketed argument denotes the number of parameters you can give to your command. "text" is the result when the macro gets expanded.

Pitfalls

\define does not use \long, that means in MkII multiple paragraphs (e.g. through \par or \endgraf) inside arguments will not work.

The name of the new command may not include numbers.

Example

\define\hello{Good morning}

% You may not use numbers
% \define[1]\mp3{The ID3 Tag of the MP3 file is: #1}
% but this will work:
\define[1]\mpThree{The ID3 Tag of the MP3 file is: #1}

% You can use non-ASCII characters in MkIV, but not in MkII.
% This example is compiled using mkiv.
\define[1]\KüFst{Küstenfunkstelle, #1!}

\starttext
\hello

\KüFst{12}

\stoptext

See also

Help from ConTeXt-Mailinglist/Forum

All issues with: