pik-piam/mip

Bug and fix in validationpdf.R: PDF may not be generated correctly if "%" in the image title

Closed this issue · 0 comments

On line 246, the validationpdf function tried to add a tex_caption to the figure.

swfigure(sw, print, p, tex_caption = preptitle(getNames(m$x)[s]), 
        sw_option = "width=10,height=2")

The function preptitle was called to generate the title from the origin name of the figure.

preptitle <- function(x) {
    x <- sub("\\.([^\\.]*)$", " (\\1)", x)
    x <- sub(".", " ", x, fixed = TRUE)
    x <- sub(" historical", "", x, fixed = TRUE)
    x <- gsub(".", " | ", x, fixed = TRUE)
    return(x)
  }

However, if there are "%" characters in the name of the figure, the pdf would not be generated successfully due to the grammar error in the generated tex file.

For example:
preptitle will generate a caption like "MAgPIE default | Agricultural Research Intensity (% of Total GDP)".
Then swfigure will add this line to the tex file: \caption{MAgPIE default | Agricultural Research Intensity (% of Total GDP)}.
It is here that the problem arises! The "%" in the caption should be preceded by a "", otherwise "% of Total GDP..." will be recognized as a latex command, resulting in an error when compiling to PDF.
This latex command is correct instead: \caption{MAgPIE default | Agricultural Research Intensity (\% of Total GDP)}

The possible solution:
In preptitle function, replace the "%" in the figure name with "percent" (or other string).
Also, I think there will be more elegant and general solutions.

The updated function may looks like:

preptitle <- function(x) {
    x <- sub("\\.([^\\.]*)$", " (\\1)", x)
    x <- sub(".", " ", x, fixed = TRUE)
    x <- sub(" historical", "", x, fixed = TRUE)
    x <- gsub(".", " | ", x, fixed = TRUE)
    x <- gsub("%", "percent", x, fixed = TRUE)
    return(x)
  }

Thank you for providing such a good data analysis tool as mip. Hope this bug can be resolved.

@pfuehrlich-pik