Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Here is my solution: hack the docx converted by Pandoc, as docx is simply a bundle of xml files and adjusting the figure sizes is pretty straightforward.</p> <p>The following is what a figure looks like in the <code>word/document.xml</code> extracted from a converted docx:</p> <pre><code>&lt;w:p&gt; &lt;w:r&gt; &lt;w:drawing&gt; &lt;wp:inline&gt; &lt;wp:extent cx="1524000" cy="1524000" /&gt; ... &lt;a:graphic&gt; &lt;a:graphicData uri="http://schemas.openxmlformats.org/drawingml/2006/picture"&gt; &lt;pic:pic&gt; ... &lt;pic:blipFill&gt; &lt;a:blip r:embed="rId23" /&gt; ... &lt;/pic:blipFill&gt; &lt;pic:spPr bwMode="auto"&gt; &lt;a:xfrm&gt; &lt;a:off x="0" y="0" /&gt; &lt;a:ext cx="1524000" cy="1524000" /&gt; &lt;/a:xfrm&gt; ... &lt;/pic:spPr&gt; &lt;/pic:pic&gt; &lt;/a:graphicData&gt; &lt;/a:graphic&gt; &lt;/wp:inline&gt; &lt;/w:drawing&gt; &lt;/w:r&gt; &lt;/w:p&gt; </code></pre> <p>So substituting the <code>cx</code> &amp; <code>cy</code> attributes of the nodes <code>wp:extent</code> &amp; <code>a:ext</code> with desired value would do the resizing job. The following R code works for me. The widest figure would take up a whole line's width specified by the variable <code>out.width</code>, and the rest are proportionally resized.</p> <pre><code>require(XML) ## default linewidth (inch) for Word 2003 out.width &lt;- 5.77 docx.file &lt;- "report.docx" ## unzip the docx converted by Pandoc system(paste("unzip", docx.file, "-d temp_dir")) document.xml &lt;- "temp_dir/word/document.xml" doc &lt;- xmlParse(document.xml) wp.extent &lt;- getNodeSet(xmlRoot(doc), "//wp:extent") a.blip &lt;- getNodeSet(xmlRoot(doc), "//a:blip") a.ext &lt;- getNodeSet(xmlRoot(doc), "//a:ext") figid &lt;- sapply(a.blip, xmlGetAttr, "r:embed") figname &lt;- dir("temp_dir/word/media/") stopifnot(length(figid) == length(figname)) pdffig &lt;- paste("temp_dir/word/media/", ## in case figure ids in docx are not in dir'ed order sort(figname)[match(figid, substr(figname, 1, nchar(figname) - 4))], sep="") ## get dimension info of included pdf figures pdfsize &lt;- do.call(rbind, lapply(pdffig, function (x) { fig.ext &lt;- substr(x, nchar(x) - 2, nchar(x)) pp &lt;- pipe(paste(ifelse(fig.ext == 'pdf', "pdfinfo", "file"), x, sep=" ")) pdfinfo &lt;- readLines(pp); close(pp) sizestr &lt;- unlist(regmatches(pdfinfo, gregexpr("[[:digit:].]+ X [[:digit:].]+", pdfinfo, ignore.case=T))) as.numeric(strsplit(sizestr, split=" x ")[[1]]) })) ## resizing pdf figures in xml DOM, with the widest figure taking up a line's width wp.cx &lt;- round(out.width*914400*pdfsize[,1]/max(pdfsize[,1])) wp.cy &lt;- round(wp.cx*pdfsize[, 2]/pdfsize[, 1]) wp.cx &lt;- as.character(wp.cx) wp.cy &lt;- as.character(wp.cy) sapply(1:length(wp.extent), function (i) xmlAttrs(wp.extent[[i]]) &lt;- c(cx = wp.cx[i], cy = wp.cy[i])); sapply(1:length(a.ext), function (i) xmlAttrs(a.ext[[i]]) &lt;- c(cx = wp.cx[i], cy = wp.cy[i])); ## save hacked xml back to docx saveXML(doc, document.xml, indent = F) setwd("temp_dir") system(paste("zip -r ../", docx.file, " *", sep="")) setwd("..") system("rm -fr temp_dir") </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload