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:
If no explicit ID is provided, NeuralSketch auto-generates one using the pattern:
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
Feature | Custom IDs | Auto-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.
Returns the ID of the block currently being drawn. Ideal for inline annotations, self-referencing, or immediate post-creation usage.
Usage:
This example annotates the block with its own ID.
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:
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.
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
Looping with Reusable IDs
You can reuse IDs deliberately within loops to conveniently reposition objects:
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.