# How to split a very long string used in a macro?

**URL:** <https://discourse.julialang.org/t/how-to-split-a-very-long-string-used-in-a-macro/96021>\
**Category:** General Usage\
**Tags:** macros, strings, makie\
**Created:** [March 13, 2023, 6:25pm UTC](https://discourse.julialang.org/t/how-to-split-a-very-long-string-used-in-a-macro/96021 "2023-03-13T18:25:54Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![g2g](https://avatars.discourse-cdn.com/v4/letter/g/41988e/32.png) [@g2g](https://discourse.julialang.org/u/g2g)\
**Post date:** [March 13, 2023, 6:25pm UTC](https://discourse.julialang.org/t/how-to-split-a-very-long-string-used-in-a-macro/96021/1 "2023-03-13T18:25:54Z")

</div>

To avoid the horizontal scrolling necessary to read a very long unsplit string, any suggestions on how to split the following very long string used in a macro?

```julia
# mwestr.jl : very long string used by a macro
using GLMakie
using Printf

function mwestr()
	# initialize figure
	fig = Figure(resolution = (500, 500))
	ax2d = Axis(fig[1,1],
		limits = (-1,1, -1,1),
		xlabel = "x (cm)",
		ylabel = "y (cm)")

	# plot initial observable data
	zCut::Float32 = 1f0
	strHeight = @sprintf( 
		"slice height = %.2f cm\nslice height = %.2f cm\nslice height = %.2f cm\nslice height = %.2f cm",
		zCut, zCut, zCut, zCut)
#	str1 = "slice height = %.2f cm\n"
#	str2 = "slice height = %.2f cm\n"
#	str3 = "slice height = %.2f cm\n"
#	str4 = "slice height = %.2f cm"
#	strHeight = @sprintf(string(str1, str2, str3, str4),
# zCut, zCut, zCut, zCut)
#	strHeight = @sprintf("$str1$str2$str3$str4",
# zCut, zCut, zCut, zCut)
	str_obs = Observable(strHeight)
	text!(ax2d, str_obs,
		position = (0.05, 0.60),
		space = :data)
	fig
	
	# generate 2D video
#	zMax = 2.0
#	nFrame = 100
#	record(fig, "mwestr.mp4", 1:nFrame) do iFrame
# zCut = zMax - abs(zMax - 2*zMax*(iFrame-1)/nFrame)
# str_obs[] = @sprintf(
# "slice height = %.2f cm\nslice height = %.2f cm\nslice height = %.2f cm\nslice height = %.2f cm",
# zCut, zCut, zCut, zCut)
#	end # for each video frame
end

```

My attempts to split the string all resulted in the following error message:

`ERROR: LoadError: MethodError: no method matching Printf.Format(::Expr)`

---

<div class="post-metadata">

**Author:** ![BioTurboNick](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bioturbonick/32/6380_2.png) [@BioTurboNick](https://discourse.julialang.org/u/BioTurboNick)\
**Post date:** [March 13, 2023, 6:31pm UTC](https://discourse.julialang.org/t/how-to-split-a-very-long-string-used-in-a-macro/96021/2 "2023-03-13T18:31:06Z")

</div>

Try this?

```julia
julia> Printf.@sprintf("test \
       me")
"test me"

```

---

<div class="post-metadata">

**Author:** ![g2g](https://avatars.discourse-cdn.com/v4/letter/g/41988e/32.png) [@g2g](https://discourse.julialang.org/u/g2g)\
**Post date:** [March 13, 2023, 6:35pm UTC](https://discourse.julialang.org/t/how-to-split-a-very-long-string-used-in-a-macro/96021/3 "2023-03-13T18:35:53Z")

</div>

That does it. Thanks.

---

<div class="post-metadata">

**Author:** ![digital\_carver](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/digital_carver/32/33818_2.png) [@digital\_carver](https://discourse.julialang.org/u/digital_carver)\
**Post date:** [March 13, 2023, 10:07pm UTC](https://discourse.julialang.org/t/how-to-split-a-very-long-string-used-in-a-macro/96021/4 "2023-03-13T22:07:29Z")

</div>

> [@g2g](#):
>
> ```julia
> @sprintf( 
> "slice height = %.2f cm\nslice height = %.2f cm\nslice height = %.2f cm\nslice height = %.2f cm",
> zCut, zCut, zCut, zCut)
> 
> ```

Since you have newlines in your string anyway, you can just enter them as actual newlines:

```julia
@sprintf( 
    """
    slice height = %.2f cm
    slice height = %.2f cm
    slice height = %.2f cm
    slice height = %.2f cm
    """,
    zCut, zCut, zCut, zCut)

```

---

<div class="post-metadata">

**Author:** ![g2g](https://avatars.discourse-cdn.com/v4/letter/g/41988e/32.png) [@g2g](https://discourse.julialang.org/u/g2g)\
**Post date:** [March 13, 2023, 10:28pm UTC](https://discourse.julialang.org/t/how-to-split-a-very-long-string-used-in-a-macro/96021/5 "2023-03-13T22:28:39Z")

</div>

Even prettier (in my opinion). Given the code will be used in an essay to showcase Julia to a different community, I’ll use your approach but both work.
