Neural Sketch

Auto-Incremental IDs

Automatically manage and reference primitives in your diagrams

NeuralSketch simplifies diagram creation by automatically assigning unique, auto-incremental IDs to every primitive you create. These IDs allow you to effortlessly reference and position elements precisely, freeing you to focus on diagram design rather than manual bookkeeping.


Understanding IDs

An ID (identifier) in NeuralSketch is essentially a unique name assigned to each primitive—like blocks, coordinates, or containers—that allows easy referencing for subsequent operations, such as relative positioning, routing or custom bridging arcs.

Neural Sketch handles IDs in two intuitive ways:

  • User-defined IDs: You explicitly specify an ID for a primitive.
  • Auto-generated IDs: NeuralSketch assigns an automatic, incrementing ID if none is provided.

Why Use IDs?

IDs provide a powerful mechanism for clean, readable diagrams. By referencing IDs, you can easily position elements relative to each other, simplifying complex layouts without manually calculating coordinates.


ID Generation

When creating any primitives, for example an \nskBlock, you can define an ID using the id key:

\nskBlock[id=myblock, ...]

If no explicit ID is provided, NeuralSketch auto-generates one using the pattern:

<shape-type><counter>

For example:

  • The first rectangle: rectangle1
  • The second diamond: diamond2
  • The third ellipse: ellipse3

These counters reset at the start of each new nskFigure environment, ensuring consistent and predictable ID assignments.

Custom vs. Auto-generated IDs

FeatureCustom IDsAuto-generated IDs
Explicit Assignment✅ Yes❌ No
Auto-incremental❌ No✅ Yes
Scoped to Figure✅ Yes✅ Yes
Overrideable✅ Yes❌ No

User-defined IDs are intentionally flexible; specifying the same ID multiple times within a figure overrides the previous definition, allowing convenient reuse in loops or iterative diagram segments.

Key Macros for ID Handling

Neural Sketch provides helpful commands to reference IDs conveniently within diagrams.

\nskBlockID

Returns the ID of the block currently being drawn. Ideal for inline annotations, self-referencing, or immediate post-creation usage.

Usage:

\nskBlock[
text-center={This block’s ID is \nskBlockID},
]

This example annotates the block with its own ID.

\nskBlockIDLast{n}

Retrieves the ID of the last-created block or, optionally, the n-th last block if {n} is provided. By default, {n=1} references the immediately previous block.

Usage:

\nskBlock[id=first, text-center={First}]
\nskBlock[pos={right=1cm of first}, text-center={Second}]
\nskBlock[pos={below=1cm of \nskBlockIDLast{1}}, text-center={Below Second}]

Here, the third block references the second block without needing to explicitly state its ID.

You can also reference earlier blocks by passing an explicit number:

tip

You can conveniently reference previously created primitives using \nskBlockIDLast, optionally specifying how far back you'd like to reference. This is especially useful when you want to position new elements relative to earlier blocks, containers, or coordinates without manually tracking or explicitly assigning IDs.

\nskBlock[pos={right=1cm of \nskBlockIDLast{2}}]

Scoping of IDs

IDs (whether manually provided or auto-generated) are scoped strictly to their figure environment (nskFigure). Once a figure environment ends, its IDs are cleared, preventing unexpected name collisions across multiple diagrams.

Internally, NeuralSketch maintains a global sequence (\g_nsk_block_id_history_seq) that tracks IDs for the duration of the current figure environment.

Technical Note

This sequence resets each time you initiate a new nskFigure. Consequently, IDs generated in one figure are inaccessible in subsequent figures unless redefined explicitly.

Practical Examples

Explicit and Auto-generated IDs

\begin{nskFigure}
% Explicit ID
\nskBlock[id=main, text-center={Main Block}]
 
% Auto-generated ID (rectangle1)
\nskBlock[pos={right=2cm of main}, text-center={Auto ID: \nskBlockID}]
 
% Reference last auto-generated block
\nskBlock[pos={below=1cm of rectangle1}, text-center={Below rectangle1}]
\end{nskFigure}

Looping with Reusable IDs

You can reuse IDs deliberately within loops to conveniently reposition objects:

\begin{nskFigure}
\foreach \x in {0,2,4} {
\nskBlock[id=iter, x=\x, y=0, text-center={ID: iter (\x)}]
% Each iteration overwrites "iter"
}
 
% This block will be positioned relative to the last "iter" (at x=4)
\nskBlock[pos={below=1cm of iter}, text-center={Below last iter}]
\end{nskFigure}

In this example, iter is reused, and the final block references the last iteration automatically.

Takeaways

Automatic and flexible ID handling in NeuralSketch reduces the mental overhead involved in diagram creation. Whether through explicit naming or automated assignments, the package streamlines how you structure, reference, and position diagram elements.

On this page