{"spec_id":"spectrum-nmr","library":"makie","language":"julia","code":"# anyplot.ai\n# spectrum-nmr: NMR Spectrum (Nuclear Magnetic Resonance)\n# Library: makie 0.22.10 | Julia 1.11.9\n# Quality: 88/100 | Created: 2026-06-03\n\nusing CairoMakie\nusing Colors\nusing Random\n\nRandom.seed!(42)\n\n# Theme tokens\nconst THEME       = get(ENV, \"ANYPLOT_THEME\", \"light\")\nconst PAGE_BG     = THEME == \"light\" ? colorant\"#FAF8F1\" : colorant\"#1A1A17\"\nconst ELEVATED_BG = THEME == \"light\" ? colorant\"#FFFDF6\" : colorant\"#242420\"\nconst INK         = THEME == \"light\" ? colorant\"#1A1A17\" : colorant\"#F0EFE8\"\nconst INK_SOFT    = THEME == \"light\" ? colorant\"#4A4A44\" : colorant\"#B8B7B0\"\nconst INK_MUTED   = THEME == \"light\" ? colorant\"#6B6A63\" : colorant\"#A8A79F\"\nconst IMPRINT_PALETTE = [\n    colorant\"#009E73\", colorant\"#C475FD\", colorant\"#4467A3\", colorant\"#BD8233\",\n    colorant\"#AE3030\", colorant\"#2ABCCD\", colorant\"#954477\", colorant\"#99B314\",\n]\n\n# Data: synthetic 1H NMR spectrum of ethanol (CH3CH2OH) at 300 MHz\nlorentz(x, x0, w, h) = h * (w / 2)^2 / ((x - x0)^2 + (w / 2)^2)\n\nn_pts    = 5000\nppm_min  = -0.5\nppm_max  = 5.5\nppm_vals = collect(range(ppm_min, ppm_max, length = n_pts))\n\nJ  = 7.0 / 300.0   # coupling constant in ppm at 300 MHz spectrometer\npw = 0.008          # Lorentzian half-width for sharp peaks (ppm)\n\n# TMS reference at 0.0 ppm (singlet)\nintensity = lorentz.(ppm_vals, 0.0, 0.006, 0.25)\n\n# CH3 triplet at 1.18 ppm — 1:2:1 relative heights\nintensity .+= lorentz.(ppm_vals, 1.18 - J,  pw, 1.0)\nintensity .+= lorentz.(ppm_vals, 1.18,      pw, 2.0)\nintensity .+= lorentz.(ppm_vals, 1.18 + J,  pw, 1.0)\n\n# OH singlet at 2.61 ppm — broader due to hydrogen-bonding exchange\nintensity .+= lorentz.(ppm_vals, 2.61, 0.018, 1.5)\n\n# CH2 quartet at 3.69 ppm — 1:3:3:1 relative heights\nintensity .+= lorentz.(ppm_vals, 3.69 - 1.5 * J, pw, 1.0)\nintensity .+= lorentz.(ppm_vals, 3.69 - 0.5 * J, pw, 3.0)\nintensity .+= lorentz.(ppm_vals, 3.69 + 0.5 * J, pw, 3.0)\nintensity .+= lorentz.(ppm_vals, 3.69 + 1.5 * J, pw, 1.0)\n\n# Baseline noise (low SNR — realistic clean spectrum)\nintensity .+= 0.015 .* randn(n_pts)\nintensity .= max.(intensity, 0.0)\n\n# Title\ntitle_str      = \"Ethanol ¹H NMR · spectrum-nmr · julia · makie · anyplot.ai\"\nn_chars        = length(title_str)\ntitle_fontsize = n_chars > 67 ? max(14, round(Int, 20 * 67 / n_chars)) : 20\n\n# Figure — landscape 3200x1800\nfig = Figure(\n    size            = (1600, 900),\n    fontsize        = 14,\n    backgroundcolor = PAGE_BG,\n)\n\nax = Axis(\n    fig[1, 1];\n    title              = title_str,\n    titlesize          = Float32(title_fontsize),\n    titlecolor         = INK,\n    xlabel             = \"Chemical Shift (ppm)\",\n    ylabel             = \"Intensity (a.u.)\",\n    xlabelcolor        = INK,\n    ylabelcolor        = INK,\n    xlabelsize         = 14,\n    ylabelsize         = 14,\n    xticklabelcolor    = INK_SOFT,\n    yticklabelcolor    = INK_SOFT,\n    xticklabelsize     = 12,\n    yticklabelsize     = 12,\n    xtickcolor         = INK_SOFT,\n    ytickcolor         = INK_SOFT,\n    backgroundcolor    = PAGE_BG,\n    topspinevisible    = false,\n    rightspinevisible  = false,\n    leftspinecolor     = INK_SOFT,\n    bottomspinecolor   = INK_SOFT,\n    xgridvisible       = false,\n    ygridvisible       = false,\n    xreversed          = true,\n    limits             = (ppm_min, ppm_max, -0.1, 3.8),\n)\n\n# Shaded area under spectrum trace (filled before the line to stay behind it)\nband!(ax, ppm_vals, zeros(n_pts), intensity; color = (IMPRINT_PALETTE[1], 0.15))\n\n# NMR spectrum trace\nlines!(ax, ppm_vals, intensity; color = IMPRINT_PALETTE[1], linewidth = 1.5)\n\n# Peak assignment labels\ntext!(ax, 1.18, 2.2;\n    text     = \"1.18 ppm\\nCH₃ (t)\",\n    align    = (:center, :bottom),\n    color    = INK_SOFT,\n    fontsize = 13,\n)\ntext!(ax, 2.61, 1.65;\n    text     = \"2.61 ppm\\nOH (s)\",\n    align    = (:center, :bottom),\n    color    = INK_SOFT,\n    fontsize = 13,\n)\ntext!(ax, 3.69, 3.15;\n    text     = \"3.69 ppm\\nCH₂ (q)\",\n    align    = (:center, :bottom),\n    color    = INK_SOFT,\n    fontsize = 13,\n)\ntext!(ax, 0.0, 0.4;\n    text     = \"TMS\",\n    align    = (:center, :bottom),\n    color    = INK_MUTED,\n    fontsize = 11,\n)\n\nsave(\"plot-$(THEME).png\", fig; px_per_unit = 2)\n"}