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

From Wiki
Jump to navigation Jump to search
m (added \doiftext, \doiftextelse)
(Added another example)
 
Line 16: Line 16:
 
* <tt>{{cmd|doifelseindented}}</tt> (LMTX only)
 
* <tt>{{cmd|doifelseindented}}</tt> (LMTX only)
  
== Example ==
+
== 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}
 +
 
 +
  \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
 +
</context>
 +
 
 +
=== Example 2: Using Lua ===
 
We can also write tests using Lua. (Example by WS, 3/2011.)
 
We can also write tests using Lua. (Example by WS, 3/2011.)
  
 
<context mode="mkiv" source="yes">
 
<context mode="mkiv" source="yes">
\starttext
+
\startTEXpage
 
   \ctxlua{test = true}
 
   \ctxlua{test = true}
 
   
 
   
Line 28: Line 55:
  
 
   Test is \ctxlua{commands.doifelse(test)}{True}{False}.
 
   Test is \ctxlua{commands.doifelse(test)}{True}{False}.
\stoptext
+
\stopTEXpage
 
</context>
 
</context>
  

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: