Difference between revisions of "Command/doif..."

From Wiki
Jump to navigation Jump to search
(Added another example)
 
(19 intermediate revisions by 7 users not shown)
Line 1: Line 1:
 
{{Reference|name=doif...|attributes=}}
 
{{Reference|name=doif...|attributes=}}
  
See [http://wiki.contextgarden.net/System_Macros/Branches_and_Decisions branches and decisions]
+
* <tt>\doifnextcharelse</tt>
 +
* <tt>\doifundefined</tt>, <tt>\doifdefined</tt>, etc.
 +
* <tt>\doif</tt>, <tt>\doifnot</tt>, <tt>\doifelse</tt>
 +
* <tt>\doifempty</tt>, <tt>\doifnotempty</tt>, <tt>\doifemptyelse</tt>
 +
* <tt>\doifinset</tt>, <tt>\doifnotinset</tt>, <tt>\doifnotinsetelse</tt>
 +
* <tt>\doifcommon</tt>, <tt>\doifnotcommon</tt>, <tt>\doifcommonelse</tt>
 +
* <tt>\doifsamestringselse</tt>,<tt>\doifsamestring</tt>,<tt>\doifnotsamestring</tt>
 +
* <tt>\doifinstringelse</tt>,<tt>\doifincsnameelse</tt>
 +
* <tt>\doifnumberelse</tt>
 +
* <tt>\doifassignmentelse</tt>
 +
* <tt>\doiftext</tt>, <tt>\doiftextelse</tt> — empty string
 +
* <tt>\doifrightpageelse</tt> (precede by {{cmd|signalrightpage}})
 +
* {{cmd|doifdocumentargument}}
 +
* <tt>{{cmd|doifelseindented}}</tt> (LMTX only)
  
Block of code depending on a Lua value - example (by WS, 3/2011):
+
== Examples ==
 +
=== Example 1: Date Macro ===
 +
<context mode="mkiv" source="yes">
 +
\startTEXpage[offset=5mm]
 +
  %--- Commands Needed
 +
  %\setvariable{NAME}{KEY}{VALUE} => \getvariable{NAME}{KEY}
 +
  %\defineexpandable[n]
 +
  %\doifnumberelse{TEXT}{TRUE_CASE}{FALSE_CASE}
  
<texcode>
+
  \defineexpandable[1]\notToday{%
\starttext
+
    \start\dorecurse{#1}{\increment\normalday}\currentdate\stop%
 +
  }%
 +
 
 +
  \defineexpandable\Date{%
 +
    \doifnumberelse{\getvariable{Document}{Date}}{\notToday{\getvariable{Document}{Date}}}{\currentdate}%
 +
  }%
 +
 
 +
  \setvariable{Document}{Date}{today}
 +
 
 +
  Today is \Date.
 +
 
 +
  \setvariable{Document}{Date}{7}
 +
 
 +
  In a week will be \Date.
 +
\stopTEXpage
 +
</context>
 +
 
 +
=== Example 2: Using Lua ===
 +
We can also write tests using Lua. (Example by WS, 3/2011.)
 +
 
 +
<context mode="mkiv" source="yes">
 +
\startTEXpage
 
   \ctxlua{test = true}
 
   \ctxlua{test = true}
 
   
 
   
  Test is \ctxlua{commands.testcase(test)}{True}{False}.
+
  Test is \ctxlua{commands.doifelse(test)}{True}{False}.
 
   
 
   
 
   \ctxlua{test = false}
 
   \ctxlua{test = false}
  
   Test is \ctxlua{commands.testcase(test)}{True}{False}.
+
   Test is \ctxlua{commands.doifelse(test)}{True}{False}.
\stoptext
+
\stopTEXpage
</texcode>
+
</context>
 +
 
 +
== See also ==
 +
 
 +
[[System Macros/Branches and Decisions|Branches and Decisions]]
 +
 
 +
== Help from ConTeXt-Mailinglist/Forum ==
 +
All issues with:
 +
{{Forum|{{SUBPAGENAME}}}}
  
[[Category:Reference/en|doif...]]
+
[[Category:Command/Internals|doif...]]

Latest revision as of 23:52, 8 September 2023

\doif...

  • \doifnextcharelse
  • \doifundefined, \doifdefined, etc.
  • \doif, \doifnot, \doifelse
  • \doifempty, \doifnotempty, \doifemptyelse
  • \doifinset, \doifnotinset, \doifnotinsetelse
  • \doifcommon, \doifnotcommon, \doifcommonelse
  • \doifsamestringselse,\doifsamestring,\doifnotsamestring
  • \doifinstringelse,\doifincsnameelse
  • \doifnumberelse
  • \doifassignmentelse
  • \doiftext, \doiftextelse — empty string
  • \doifrightpageelse (precede by \signalrightpage)
  • \doifdocumentargument
  • \doifelseindented (LMTX only)

Examples

Example 1: Date Macro

\startTEXpage[offset=5mm]
  %--- Commands Needed
  %\setvariable{NAME}{KEY}{VALUE} => \getvariable{NAME}{KEY}
  %\defineexpandable[n]
  %\doifnumberelse{TEXT}{TRUE_CASE}{FALSE_CASE}

  \defineexpandable[1]\notToday{%
    \start\dorecurse{#1}{\increment\normalday}\currentdate\stop%
  }%
  
  \defineexpandable\Date{%
    \doifnumberelse{\getvariable{Document}{Date}}{\notToday{\getvariable{Document}{Date}}}{\currentdate}%
  }%

  \setvariable{Document}{Date}{today}

  Today is \Date.
  
  \setvariable{Document}{Date}{7}
  
  In a week will be \Date.
\stopTEXpage

Example 2: Using Lua

We can also write tests using Lua. (Example by WS, 3/2011.)

\startTEXpage
  \ctxlua{test = true}
 
   Test is \ctxlua{commands.doifelse(test)}{True}{False}.
 
  \ctxlua{test = false}

  Test is \ctxlua{commands.doifelse(test)}{True}{False}.
\stopTEXpage

See also

Branches and Decisions

Help from ConTeXt-Mailinglist/Forum

All issues with: