What is the correct way to use a for loop plotting within a lilaq diagram?

The documentation demonstrates multiple plots in a diagram by listing the lq.plot(...), calls as arguments to lq.diagram (i.e. separated by commas). If one wants to plot multiple lines in a loop, is there a way to do so? For example, consider the code below:


#let loop_lq(A) = {
  
  import "@preview/lilaq:0.2.0" as lq
  let xs = lq.linspace(0, 1, num: 11)
  let fn(x, a: 0.0) = calc.sin(a * x)

  lq.diagram(
    width: 10cm, 
    height: 5cm,
    ylim: (0, auto),
    xlim: (0, 1),
    legend:(position: left+top),

    for a in A {
      lq.plot(
        xs, mark: none, 
        xs.map(t => fn(t, a:a)), 
        stroke: (paint: colors.tertiary.lighten(25%).rotate(calc.log(a)*40deg), thickness: 3pt),
        //tip: tiptoe.stealth,
        //toe: tiptoe.bar,
        label: [$a = #a$],
      )
    }
  )
}

#loop_lq((0.1, 1.0))

This produces an output which is missing all but the last iteration.

Hi @Christopher_Marcotte ,

you can achieve this with unpacking of arrays, like

#import "@preview/lilaq:0.2.0" as lq

#let x = lq.linspace(0, 10)
#lq.diagram(
  ..range(6).map(i => lq.plot(x, x.map(x => x + i)))
)

In your example, you would replace range(6) with your array A. Each element is mapped to a lq.plot instance which results in an array of plots.

With the .. unpacking syntax, you can pass the array items as individual arguments to lq.diagram.

2 Likes