咨询:在rmdformats::material主题中无法调整图片宽度的解决办法
问题分析与解决方法
你遇到的问题确实和rmdformats::material主题的默认CSS样式有关——该主题会给图片添加max-width: 100%的限制,强制图片宽度不超过父容器的宽度,导致你设置的out.width参数被覆盖,只能调整高度。以下是几种可行的解决方法:
方法一:直接在代码块中强制设置图片样式
在R代码块中添加out.extra参数,通过内联CSS强制指定图片宽度,优先级高于主题默认样式:
{r, echo=FALSE, out.height="300px", fig.align="center", fig.cap="abc", out.extra='style="width:200px !important;"'} library(knitr) knitr::include_graphics("img.png")
方法二:自定义CSS修改主题边距与图片限制
在文档开头添加自定义CSS块,既能调整页面容器的边距(间接扩大图片可使用的宽度范围),也能取消或修改图片的宽度限制:
--- title: "Long Non-Coding RNAs" author: "Name" date: "date" output: rmdformats::material: highlight: "tango" code_folding: hide number_sections: yes --- <style> /* 调整页面主容器的宽度和边距 */ .container { max-width: 1200px !important; /* 替换为你需要的最大宽度 */ width: 95% !important; /* 占视口宽度的比例 */ } /* 取消图片的最大宽度限制,让out.width参数生效 */ img { max-width: none !important; } </style>
之后再使用out.width参数设置图片宽度即可生效:
{r, echo=FALSE, out.width="200px", out.height="300px", fig.align="center", fig.cap="abc"} library(knitr) knitr::include_graphics("img.png")
方法三:修改主题的全局CSS文件(进阶)
如果你需要长期使用自定义样式,可先通过system.file("rmdformats/templates/material/css/material.min.css", package = "rmdformats")查看主题CSS文件路径,复制一份到项目目录后修改其中的.container和img相关样式,最后在YAML中指定自定义CSS:
output: rmdformats::material: highlight: "tango" code_folding: hide number_sections: yes css: "custom-material.css"
内容的提问来源于stack exchange,提问作者Ian hong Lee
相关产品推荐
相关产品推荐

