+#define INPUT_TO_Y(i) (((double) (i)) * vertical_spacing)
+
+static void exit_usage (void) /* {{{ */
+{
+ printf ("Usage: sn-tex [options] [file]\n"
+ "\n"
+ "Valid options are:\n"
+ " -w <width> Specify the width of the graph (in TikZ default units).\n"
+ "\n");
+ exit (EXIT_FAILURE);
+} /* }}} void exit_usage */
+
+static int read_options (int argc, char **argv) /* {{{ */
+{
+ int option;
+
+ while ((option = getopt (argc, argv, "w:h?")) != -1)
+ {
+ switch (option)
+ {
+ case 'w':
+ {
+ double width = atof (optarg);
+ if (width <= 0.0)
+ {
+ fprintf (stderr, "Invalid width argument: %s\n", optarg);
+ exit (EXIT_FAILURE);
+ }
+ output_width = width;
+ break;
+ }
+
+ case 'h':
+ case '?':
+ default:
+ exit_usage ();
+ }
+ }
+
+ return (0);
+} /* }}} int read_options */
+
+static double determine_stage_width (sn_stage_t *s) /* {{{ */
+{
+ int lines[s->comparators_num];
+ int right[s->comparators_num];
+ int lines_used = 0;
+ int i;
+
+ if (SN_STAGE_COMP_NUM (s) == 0)
+ return (0.0);
+
+ for (i = 0; i < SN_STAGE_COMP_NUM (s); i++)
+ {
+ lines[i] = -1;
+ right[i] = -1;
+ }
+
+ for (i = 0; i < SN_STAGE_COMP_NUM (s); i++)
+ {
+ int j;
+ sn_comparator_t *c = SN_STAGE_COMP_GET (s, i);
+
+ for (j = 0; j < lines_used; j++)
+ if (SN_COMP_LEFT (c) > right[j])
+ break;
+
+ lines[i] = j;
+ right[j] = SN_COMP_RIGHT (c);
+ if (j >= lines_used)
+ lines_used = j + 1;
+ }
+ assert (lines_used >= 1);
+
+ return (((double) (lines_used - 1)) * inner_spacing);
+} /* }}} double determine_stage_width */
+
+static double determine_network_width (sn_network_t *n) /* {{{ */
+{
+ double width;
+ int i;
+
+ /* Spacing between stages and at the beginning and end of the network */
+ width = (SN_NETWORK_STAGE_NUM (n) + 1) * outer_spacing;
+
+ /* Spacing required within a stage */
+ for (i = 0; i < SN_NETWORK_STAGE_NUM (n); i++)
+ width += determine_stage_width (SN_NETWORK_STAGE_GET (n, i));
+
+ return (width);
+} /* }}} double determine_network_width */
+
+static int tex_show_stage (sn_stage_t *s) /* {{{ */