0

I need to automatically change the plurality of the Student(s), Supervisor(s) and Co-Supervisor(s) depending on the array length. Note the authors array will have to contain atleast one entry (string) the supervisor and co-supervisor do not. I tried using if statements, while inside the stack, but it keeps giving me syntax error, and i do not understand how else to write it.

grid(
  columns: 2,
  column-gutter: if authors.len() > 0 and authors.len() > 20{
  3em
  }else{
  7em  
  },
  // Students column
  stack(
    dir: ttb,
    spacing: 0.7em,
    if authors.len() > 1{
      align(start, emph("Students:")),
      ..authors.map(author => align(start, author))   
    }else{
      align(start, emph("Student:")),
      align(start, authors.first())
    }),
  // Supervisors column
  stack(
    dir: ttb,
    spacing: 0.7em,
      align(start, emph("Supervisors:")),
      ..supervisors.map(supervisor => align(start, supervisor)),
      // Co-Supervisors
      align(start, emph("Co-Supervisor:")),
      ..co-supervisors.map(co-supervisor => align(start, co-supervisor))
  ),
)

2 Answers 2

0

Here is a not-very-elegant solution:

stack(
  dir: ttb,
  spacing: 0.7em,
  ..{
    if authors.len() > 1{
      (align(start, emph("Students:")),) + authors.map(author => align(start, author))
    } else {
      (align(start, emph("Student:")), align(start, authors.first()))
    }
  }
),

We return the array in the if-else block and extract contents using .. outside of the if-else block.

Sign up to request clarification or add additional context in comments.

Comments

0

If you just want to change the text but everything else (e.g. grid layout) should stay the same, then it would be easiest to use the if-else expression just for the text ("Student" / "Students"):

stack(
  dir: ttb,
  spacing: 0.7em,
  align(start, emph(
    if authors.len() == 1 {"Student"} else {"Students"} + ":"
  )),
  ..authors.map(author => align(start, author)),
)

The problem with your code was that inside the { ... } of the if-else you tried to have two expressions as value, which does not work. You would have to put them inside an array and then spread them again (using ..), as shown in the other answer.
Here is a simplified example:

stack(
  ..if true {
     ("a", "b")
   } else {
     ("c", "d")
   }
)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.