Neural Sketch
Commands/preloaded

nsk@Lerp

Linear interpolation for computing intermediate percentage values in Neural Sketch.

Overview

The \nskLerp command computes a linear interpolation between two numeric endpoints across a specified number of elements. It is useful for dynamically varying styling parameters—such as opacity, color mixing, or dimension scaling—based on an element's position in a sequence.

Internally, \nskLerp maps an index i in the range [1..n] to a percentage value between from and to. If the to value is between 0 and 1, it is interpreted as a fraction and scaled to 0–100.

Basic Usage

% Compute the midpoint (50%) between 0 and 100:
\nskLerp[from=0, to=100, n=2]{2}
% → 100.000000 (i=2 gives max)

By default, the computed percentage is typeset directly. To store the result in a macro instead, use the into key.

Example in context

In the rqt.tex example, \nskLerp drives a color mix across four blocks:

\foreach \i in {1,...,4} {
  \nskLerp[from=100, to=30, n=4, into=p]{\i}
  \nskBlock[id=eb\i, fill=nskSecondaryAccent!\p!nskBg]
}

Here, p will hold values 100, ~76.67, ~53.33, and 30, producing a fading color effect.

Available Options

  • from (float, default=0.0): Starting value of the interpolation range.
  • to (float, default=100.0): Ending value of the interpolation range. Values ≤1 are treated as fractions and scaled to 0–100.
  • n (integer, default=1): Total number of interpolation steps (elements).
  • into (token list, default={}): Name of a macro to store the computed percentage. If unspecified, the value is typeset directly.

When n=1 or if the index is 1, \nskLerp returns the from value; for index n, it returns the to value.

Internal Behavior

  • Scales to and from by 100 if the provided to value ≤1.
  • Computes:
    pct = from + (i - 1) / (n - 1) * (to - from)
  • Converts the result to a decimal string for typesetting or assignment.

Reference Table

PropTypeDefault
from?float0.0
to?float100.0
n?integer1
into?token list{}

On this page