How to change image-var based on bool?

Hi,
I have a typ document with 2k lines. At several sections I have defined variables with content/images.
I now what to change the image dependend on a bool.
However I cannot use the var myimage2 due to the scope …
Any recommendation how to solve this?
Many thanks …

#let isLevelEasy = true

= section 1
#let myimage1 = image("image1")
// use myimage1 here
myimage1

= section 2
// I would like to define myimage2 as part of section2
#if isLevelEasy {
  let myimage2 = image("image2")
} else {
  let myimage2 = image("image2_Easy")
}
// I would like to use myimage2 here ... 

You can declare the myimage2 variable before the if block:

= section 2
//Create the variable
#let myimage2 = none
//Store an image in the variable
#if isLevelEasy {
  myimage2 = image("image2")
} else {
  myimage2 = image("image2_Easy")
}
//Display it here
#myimage2

It can also be shortened by deciding which to use when declaring the variable:

= section 2
#let myimage2 = if isLevelEasy {image("image2")} else {image("image2_Easy")}
#myimage2

Or, if you don’t actually need to save the image in a variable, you can simply return the correct image from within the if block which will then be displayed:

= section 2
#if isLevelEasy {
  image("image2")
} else {
  image("image2_Easy")
}
1 Like

Hi @gezepi,
the “shortened version” is an elegant way to solve my problem.
Thank you for your support …

Or even shorter, if same arguments used:

#let myimage2 = image(if isLevelEasy { "image2" } else { "image2_Easy" })