Create true length scalebar for different latitudes with QGIS geometry generator in Mercator projection

In Mercator projection, distances are heavily distorted, the closer you get to the poles. True distances can only be measured only on the equator. The distortion is constant along parallels - so e.g. along the parallel at 60 degrees north the true length of the line (ellipsoidal distance/distance on Earth's curved surface) can be calculated multiplying the cartesian length with a coeficient for this latitude.

Based on this principle, how is it possible to create true distances on a line layer (containing only parallels) with QGIS expressions/Geometry generator? The idea is to create kind of scalebar that shows the varying (cartesian) distance at different latitudes that corresponds to the same real-world distance (ellipsoidal length)?

Based on this answer, the formula to convert length L in Mercator projection to ellipsoidal distance is L / cos(θ), where θ is the latitude (in radians).

Thus if you have a line following a parallel, defined in a CRS based on Mercator projection (here I use Web Mercator EPSG:3857), you can use the following expression. It creates a variable (here: 100, can be changed on line 3) points/markers every 100 km along the line (this interval can be changed on line 8).

Ellipsoidal measurement at 80 degrees North returns ca. 400 km for the distance of four intervals created with the expression. The closer you come to the equator, the smaller the cartesian (distance on planar map canvas, projected in Web Mercator) distance gets:

collect_geometries(
array_foreach (
generate_series (0,100,1),  -- 100 = no. of markers per line to generate; change here
end_point(
line_substring (
$geometry, 0, @element*100000/ -- 100 km intervals; change here cos ( radians ( y ( transform ( start_point ($geometry),
'EPSG:3857', 'EPSG:4326'
)
)
)
)
)
)
)
)