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

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?

# 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)

Try this?

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

That does it. Thanks.

1 Like

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

@sprintf( 
    """
    slice height = %.2f cm
    slice height = %.2f cm
    slice height = %.2f cm
    slice height = %.2f cm
    """,
    zCut, zCut, zCut, zCut)
3 Likes

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.