Neural Sketch

Conditional Rendering

Dynamically render content in diagrams based on conditional logic.

Conditional rendering in Neural Sketch enables diagrams to dynamically adapt based on logical conditions, enhancing flexibility and expressiveness. Built on robust LaTeX3\LaTeX3 layer constructs and syntax, it provides a reliable and efficient system for conditional evaluation and rendering.

This powerful mechanism lets you render specific diagram elements conditionally, simplifying the creation of diagrams that adjust to various states or inputs.

Overview

Conditional rendering in Neural Sketch is primarily managed through two macros:

  • \nskSwitch: Defines a conditional context.
  • \nskCase and \nskDefault: Handle specific cases within this context.

Each switch evaluates an expression against defined cases, rendering content accordingly.

Syntax

Here's the general structure:

\begin{nskSwitch}[type=<type>]{<value>}
  \nskCase[<case-value>]{Content for this case}
  \nskDefault{Default content if no case matches}
\end{nskSwitch}
  • <type> can be int, string, float, or bool.
  • <value> is the expression evaluated by the switch.
  • <case-value> defines individual cases to match against <value>.

Examples

Basic Example

The following example illustrates conditional rendering for different data types:

\begin{nskSwitch}[type=int]{42}
  \nskCase[10]{This is case 10.}
  \nskCase[43]{This is the matching case for 42.}
  \nskDefault{No case matched.}
\end{nskSwitch}
Conditional Rendering Example

Conditional Diagram Components

Conditional rendering integrates seamlessly within diagrams, dynamically altering visual components based on conditions:

\foreach \c in {Green, Yellow, dots, skip} {
  \begin{nskSwitch}[type=string]{\c}
    \nskCase[skip]{
      \nskBlock[
        last-pos-s={right=0cm},
        border-type=dashed,
        shadow=false,
      ]
    }
    \nskCase[dots]{
      \nskBlock[
        last-pos-s={right=0cm},
        text-center=$\cdots$,
        border-type=none, fill=none,
        shadow=false,
      ]
    }
    \nskDefault{
      \nskBlock[
        last-pos-s={right=.5cm},
        text-north=\c,
        fill=nsk\c,
      ]
    }
  \end{nskSwitch}
}
Conditional Diagram Example

Best Practices

  • Clear cases: Clearly define each case with descriptive names or values.
  • Default cases: Always include a default case to handle unexpected conditions gracefully.
  • Type consistency: Ensure the type matches the evaluated value and case values to avoid mismatches.

Using Conditional Rendering as If-Else

You can easily replicate simple if-else logic by providing only one case and the default. This structure behaves exactly like an if-else statement:

\begin{nskSwitch}[type=string]{color}
  \nskCase[red]{Condition met, rendering this content.}
  \nskDefault{Condition not met, rendering default content.}
\end{nskSwitch}

This approach simplifies scenarios that only require binary (yes/no, true/false) decision-making.

Advanced Usage

You can nest conditional switches for more complex diagrams, enabling multi-level conditional rendering:

\begin{nskSwitch}[type=bool]{true}
  \nskCase[true]{
    \begin{nskSwitch}[type=string]{optionA}
      \nskCase[optionA]{Nested option A}
      \nskCase[optionB]{Nested option B}
      \nskDefault{Nested default option}
    \end{nskSwitch}
  }
  \nskDefault{Outer default case}
\end{nskSwitch}

This example demonstrates conditional rendering within nested contexts, enabling sophisticated diagram logic.

Reference

PropTypeDefault
type?choicestring

Key Takeaway

Conditional rendering significantly enhances diagram flexibility, allowing you to build dynamic, responsive, and adaptable visuals with ease.

On this page