From fa4a1a629d8cf8efb5ffe7af05d4600cb96b6062 Mon Sep 17 00:00:00 2001 From: Sebastian Pech Date: Sun, 25 Aug 2019 19:19:31 +0200 Subject: [PATCH] Add function to convert percentages to matching latex widths --- src/formatters.jl | 14 ++++++++++++-- test/figureformatter_test.jl | 17 +++++++++++++---- 2 files changed, 25 insertions(+), 6 deletions(-) diff --git a/src/formatters.jl b/src/formatters.jl index 7aeb3dc..d0c90e4 100644 --- a/src/formatters.jl +++ b/src/formatters.jl @@ -243,6 +243,16 @@ const adoc = AsciiDoc("AsciiDoc", :doctype => "asciidoc" )) +function md_length_to_latex(def,reference) + if occursin("%",def) + _def = tryparse(Float64,replace(def,"%"=>"")) + _def == nothing && return def + perc = round(_def/100,digits=2) + return "$perc$reference" + end + return def +end + function formatfigures(chunk, docformat::Union{Tex,JMarkdown2tex}) fignames = chunk.figures caption = chunk.options[:fig_cap] @@ -260,9 +270,9 @@ function formatfigures(chunk, docformat::Union{Tex,JMarkdown2tex}) (f_pos == nothing) && (f_pos = "!h") #Set size attribs = "" - width == nothing || (attribs = "width=$width") + width == nothing || (attribs = "width=$(md_length_to_latex(width,"\\linewidth"))") (attribs != "" && height != nothing ) && (attribs *= ",") - height == nothing || (attribs *= "height=$height") + height == nothing || (attribs *= "height=$(md_length_to_latex(height,"\\paperheight"))") if f_env != nothing result *= "\\begin{$f_env}" diff --git a/test/figureformatter_test.jl b/test/figureformatter_test.jl index 6ee63cf..c85d82e 100644 --- a/test/figureformatter_test.jl +++ b/test/figureformatter_test.jl @@ -21,9 +21,9 @@ chunk.options[:out_width] = "100%" @test Weave.formatfigures(chunk, Weave.rst) == ".. image:: figs/figures_plot1.png\n :width: 100%\n\n" chunk.options[:fig_cap] = "Nice plot" -@test Weave.formatfigures(chunk, Weave.tex) == "\\begin{figure}[!h]\n\\center\n\\includegraphics[width=100%]{figs/figures_plot1.png}\n\\caption{Nice plot}\n\\end{figure}\n" +@test Weave.formatfigures(chunk, Weave.tex) == "\\begin{figure}[!h]\n\\center\n\\includegraphics[width=1.0\\linewidth]{figs/figures_plot1.png}\n\\caption{Nice plot}\n\\end{figure}\n" @test Weave.formatfigures(chunk, Weave.pandoc) == "![Nice plot](figs/figures_plot1.png){width=100%}\n" -@test Weave.formatfigures(chunk, Weave.md2tex) == "\\begin{figure}[!h]\n\\center\n\\includegraphics[width=100%]{figs/figures_plot1.png}\n\\caption{Nice plot}\n\\end{figure}\n" +@test Weave.formatfigures(chunk, Weave.md2tex) == "\\begin{figure}[!h]\n\\center\n\\includegraphics[width=1.0\\linewidth]{figs/figures_plot1.png}\n\\caption{Nice plot}\n\\end{figure}\n" @test Weave.formatfigures(chunk, Weave.md2html) == "
\n\n
Nice plot
\n
\n" @test Weave.formatfigures(chunk, Weave.rst) == ".. figure:: figs/figures_plot1.png\n :width: 100%\n\n Nice plot\n\n" @test Weave.formatfigures(chunk, Weave.multimarkdown) == "![Nice plot][figs/figures_plot1.png]\n\n[figs/figures_plot1.png]: figs/figures_plot1.png width=100%\n" @@ -31,11 +31,20 @@ chunk.options[:fig_cap] = "Nice plot" chunk.options[:label] = "somefig" @test Weave.formatfigures(chunk, Weave.pandoc) == "![Nice plot](figs/figures_plot1.png){width=100% #fig:somefig}\n" -@test Weave.formatfigures(chunk, Weave.tex) == "\\begin{figure}[!h]\n\\center\n\\includegraphics[width=100%]{figs/figures_plot1.png}\n\\caption{Nice plot}\n\\label{fig:somefig}\n\\end{figure}\n" +@test Weave.formatfigures(chunk, Weave.tex) == "\\begin{figure}[!h]\n\\center\n\\includegraphics[width=1.0\\linewidth]{figs/figures_plot1.png}\n\\caption{Nice plot}\n\\label{fig:somefig}\n\\end{figure}\n" @test Weave.formatfigures(chunk, Weave.tex) == Weave.formatfigures(chunk, Weave.md2tex) chunk.options[:label] = nothing chunk.options[:fig_cap] = nothing chunk.options[:fig_env] = "center" chunk.options[:fig_pos] = "" -@test Weave.formatfigures(chunk, Weave.tex) == "\\begin{center}\n\\includegraphics[width=100%]{figs/figures_plot1.png}\n\\end{center}\n" +@test Weave.formatfigures(chunk, Weave.tex) == "\\begin{center}\n\\includegraphics[width=1.0\\linewidth]{figs/figures_plot1.png}\n\\end{center}\n" + +chunk.options[:out_width] = "50%" +chunk.options[:out_height] = "75 %" +@test Weave.formatfigures(chunk, Weave.tex) == "\\begin{center}\n\\includegraphics[width=0.5\\linewidth,height=0.75\\paperheight]{figs/figures_plot1.png}\n\\end{center}\n" + +chunk.options[:out_width] = "A%" +chunk.options[:out_height] = "0.5\\textwidth" +@test Weave.formatfigures(chunk, Weave.tex) == "\\begin{center}\n\\includegraphics[width=A%,height=0.5\\textwidth]{figs/figures_plot1.png}\n\\end{center}\n" +