Search code examples
juliasuperscriptgadfly

How to modify superscripts in Julia (labels of Gadfly plots)?


Could somebody suggest me how to initiate and end the superscript in a Gadfly plot using Julia correctly? Or if there are any other better solutions instead of using <sup></sup>, please also share them with me. Thanks so much.

using Gadfly
ticks = [400, 1000, 1500, 2000, 2500, 3000, 3500, 4000]
Gadfly.plot(PET, x = :Wavenumber, y = :Transmittance, Geom.line, 
            Coord.cartesian(xflip=true, xmin=400, xmax=4000, ymax=100, ymin=0),
            Scale.x_continuous(maxticks=2000), 
            Guide.xticks(ticks=ticks),
            Guide.xlabel("Wavenumber (cm<sup>-1</sup>)"),
            Guide.ylabel("Transmittance (%)"))

enter image description here


Solution

    1. In a supported text editor, you can type the superscript characters directly with tab completion. \^-1<Tab> should transform into ⁻¹ once you press <Tab>.
    2. Copy the characters from this answer to your string: ⁻¹.
    3. Use my function below to convert integers to their corresponding Unicode superscripts. Use the * function to join the output string with the rest of your xlabel.
    "Finds and prints the appropraite unicode value for any numerical superscript."
    function superscriptnumber(i::Int)
        if i < 0
            c = [Char(0x207B)]
        else
            c = []
        end
        for d in reverse(digits(abs(i)))
            if d == 0 push!(c, Char(0x2070)) end
            if d == 1 push!(c, Char(0x00B9)) end
            if d == 2 push!(c, Char(0x00B2)) end
            if d == 3 push!(c, Char(0x00B3)) end
            if d > 3 push!(c, Char(0x2070+d)) end
        end
        return join(c)
    end