Positioning Mechanisms
An in-depth guide to absolute and relative positioning in NeuralSketch, essential for creating precise and visually coherent diagrams.
Positioning is fundamental to crafting clear, accurate, and visually appealing diagrams. NeuralSketch provides robust, flexible, and intuitive positioning mechanisms designed for both straightforward and complex diagramming tasks.
Overview
NeuralSketch positions diagram elements using two primary methods:
- Absolute positioning: Explicitly define exact coordinates.
- Relative positioning: Place elements in relation to other diagram primitives, containers, groups, or blocks.
Each method supports nuanced control through additional positioning keys, allowing you to precisely adjust or align elements with minimal boilerplate.
Absolute Positioning
Absolute positioning allows you to define the exact coordinates for placing a primitive on the canvas. This method is ideal for scenarios requiring explicit spatial precision.
Basic Usage
Define absolute positions using the x
and y
keys, representing horizontal and vertical coordinates respectively:
Coordinate Shifting
After establishing an absolute position, you can apply additional fine-grained adjustments using shift-x and shift-y keys:
This moves the block horizontally to the right by 0.5 units and vertically downward by 0.25 units, relative to the specified coordinates.
Relative Positioning
Relative positioning aligns primitives with reference to other primitives by ID
, significantly simplifying complex diagram layouts.
Using the pos
Keyword
NeuralSketch’s pos
key uses natural-language-inspired syntax, based on TikZ’s positioning library. This allows expressive, readable placements without numerical guesswork:
Here, the second block (bblock
) is positioned exactly 1 cm above and 0.5 cm to the right of the first block (ablock
).
ERROR-BadIndex
The pos
keyword raises a custom error, ERROR-BadIndex
, when an invalid reference ID is provided. The error message explicitly identifies the problematic ID and indicates if it is out of bounds or undefined.
Using the last-pos Keyword
When positioning a series of primitives sequentially, use the last-pos
key to easily chain elements relative to the previously drawn primitive:
This syntax intuitively chains the second and third blocks, significantly streamlining diagram creation and removing the need to manually declare or reference IDs.
Error Handling with last-pos
When employing loops or conditional logic, the previous primitive might not always exist. Using the standard last-pos
would produce errors if no preceding primitive is defined. NeuralSketch elegantly handles this with the last-pos-s
(safe) key.
Using last-pos-s
The last-pos-s
key gracefully handles missing references by applying the position transformation only if a valid registered primitive exists
On the first iteration, since no preceding block exists, the command simply has no effect. Subsequent blocks correctly position relative to their predecessors.
Custom Errors with last-pos
If you explicitly use last-pos
and the referenced block does not exist, NeuralSketch will issue a custom error clearly indicating the missing or invalid index, aiding debugging.
Intelligent Alignment
NeuralSketch intelligently infers alignment intentions, simplifying diagrams that rely heavily on aligned elements.
For example, horizontally aligning adjacent blocks does not require explicit anchors—NeuralSketch automatically chooses appropriate edges:
In this scenario, NeuralSketch aligns the left edge of Block B with the right edge of Block A, automatically interpreting your intent without needing explicit anchors.
Combining Positioning with Containers and Groups
Positioning mechanisms extend naturally to containers and groups, allowing cohesive positioning and transformation of composite diagram components.
Containers
Containers wrap multiple primitives, allowing global transformations alongside internal relative positioning while providing handy stylings:
This container moves as a unit, while blocks inside maintain local relative positioning.
Groups
Groups logically cluster primitives, making collective transformations (e.g., rotation or scaling) straightforward while maintaining local positional consistency:
Both blocks rotate cohesively, preserving their relative positions within the transformed group.
Best Practices
- Absolute positioning is ideal for single, fixed, or foundational elements.
- Relative positioning reduces redundancy and complexity in interconnected or iterative diagrams.
- Use
last-pos-s
within loops or uncertain contexts to avoid unnecessary errors gracefully. - Use NeuralSketch’s intelligent alignment for cleaner, simpler code.
Takeaways
NeuralSketch’s versatile positioning mechanisms—combining absolute coordinates, expressive relative references, intelligent alignment inference, and robust error handling—offer unmatched flexibility and ease-of-use, empowering you to create precise, sophisticated, and visually coherent diagrams with minimal effort.