Difference between revisions of "Dimensions"

From Wiki
Jump to navigation Jump to search
Line 61: Line 61:
  
 
== Important to understand ==
 
== Important to understand ==
ToDos: This points have to be explained.
+
{{todo|This points have to be explained.}}
  
 
===Which method? ===
 
===Which method? ===
When to use which of the above methods?
+
{{todo|When to use which of the above methods?}}
  
 
=== \the ===
 
=== \the ===
\the: what is it? When to use?
+
{{todo|\the: what is it? When to use?}}
  
 
=== Expanding ===
 
=== Expanding ===
expanding of variables: What does this mean? When to use?
+
{{todo|expanding of variables: What does this mean? When to use?}}
  
 
=== \relax ===
 
=== \relax ===
Line 108: Line 108:
 
[[Category:ConTeXt-Issues]]
 
[[Category:ConTeXt-Issues]]
 
[[Category:Programming]]
 
[[Category:Programming]]
 +
[[Category:ToDo]]

Revision as of 20:39, 23 April 2013

ConTeXt offers different methods to initialize variables and to calculate with dimension units.

\definemeasure

\setupexternalfigures[location={local,global,default}]

\definemeasure[ColumnC][30mm]
\definemeasure[ColumnD][40mm]
\definemeasure[ColumnCD][\the\dimexpr(\measure{ColumnC}+\measure{ColumnD})\relax]

C \measure{ColumnC}

D \measure{ColumnD}

CD \measure{ColumnCD}

\externalfigure[cow][width=\measure{ColumnCD}]


\defineexpandable

\setupexternalfigures[location={local,global,default}]

\defineexpandable\ColumnA    {30mm}
\defineexpandable\ColumnB    {40mm}
\defineexpandable\ColumnAB    {\the\dimexpr(\ColumnA + \ColumnB)\relax}

A  \ColumnA

B  \ColumnB

AB: \ColumnAB

\externalfigure[cow][width=\ColumnAB]


The behavior of \define has changed. At first the default was expanded, now it is unexpanded. If "unexpanded" is wished some extracommands are neccesary:

\defineexpandable\...
same as the lowlevel-TeX-Macro \def\...
\define\...
same as the low-level-textmacro \unexpanded\def\... 

\def

\newdimen

\setupexternalfigures[location={local,global,default}]

\defineexpandable\ColumnA    {30mm}
\defineexpandable\ColumnB    {40mm}
\newdimen\ColumnAB \ColumnAB=\dimexpr(\ColumnA + \ColumnB)

A  \ColumnA

B  \ColumnB

ColumnAB: \the\ColumnAB

\externalfigure[cow][width=\the\ColumnAB]

\externalfigure ist also working without "\the".


Important to understand


TODO: This points have to be explained. (See: To-Do List)


Which method?


TODO: When to use which of the above methods? (See: To-Do List)


\the


TODO: \the: what is it? When to use? (See: To-Do List)


Expanding


TODO: expanding of variables: What does this mean? When to use? (See: To-Do List)


\relax

With \relax the termination of a calculation is garantied. If there is one too much ... no problem, then it is ignored.

"add a \relax after code definitions to prevent the scanner from reading too much"

\the\dimexpr... will look ahead till there's something nonexpandable, and contrary to what one expects

\dimexpr(...)


will not stop at the ) so, although with measures it normally goes okay, personally I always use \relax as terminator; the \relax will be eaten up so there is no interference but it guarantees to stop scanning.

Example
\definemeasure[ColumnC][30mm]
\definemeasure[ColumnD][40mm]
\definemeasure[ColumnCDi][\the\dimexpr(\measure{ColumnC}+\measure{ColumnD})\relax]


Further examples
\footnote{\input tufte \relax} \par
\definepapersize[bigsheet][width=\dimexpr\paperwidth+6mm\relax,height=\dimexpr\paperheight+6mm\relax]
\setupTABLE[c][3][width={\dimexpr(\textwidth-2cm)* 2 / 3\relax}]
\definemeasure[topspace] [\dimexpr(210mm-31mm*5)/2\relax]

Other ways to calculate (without dimension units)

\ctxlua

\defineexpandable\ValueA {20}
\defineexpandable\ValueB {30}
\defineexpandable\ValueC {\ctxlua{context(\ValueA + \ValueB)}}

\ValueC

See also