R markdown (使用 CTEX documents 模板)中可以实现 latex 命令中加入 R code,如下:(为了显示的需要 ``` 符号用 ---代替;)

\begin{figure}
---{r, out.width='50%', fig.align="center", echo = FALSE}
  knitr::include_graphics("images/fig.png")
---
\caption{中文题目}
\end{figure}

如果反过来 R code 中是否可以加入 latex 命令?如果可以,应该如何操作(下面的代码无法执行)

```{r, out.width='50%', fig.align="center", echo = FALSE}
\begin{figure}
  knitr::include_graphics("images/fig.png")
\caption{中文题目}
\end{figure}
---
    1. R Markdown 是基于 Pandoc Markdown,是 Pandoc 转化器对 Markdown 的支持,这种支持包括 Markdown 文本里插入 LaTeX 代码,至少认真把网页 https://pandoc.org/MANUAL.html#pandocs-markdown 看一遍,你就会明白了

    2. 如果需要在 R 代码块执行后,对图有特殊的排版要求,有种做法就是对生成的 LaTeX 文本进行替换、插入一些 LaTeX 命令,举个例子 https://github.com/rstudio/rmarkdown-book/blob/master/index.Rmd#L30-L53 代码块控制表格16.1 的输出样式 https://github.com/rstudio/rmarkdown-book/blob/master/16-widgets.Rmd#L374-L394

    3. 这是个比较高级的魔法,需要会一点正则表达式,谢大的这个例子,没有启用 raw strings 匹配,导致转义符 \ 超多,你得慢慢理解

    4. 最后就你这个具体的问题,谢大的 knitr 包已经提供超多的图形格式控制,见 https://yihui.name/knitr/options/#plots 其中,fig.cap="中文题目" 即可

    附:
    表格转化前

    \begin{longtable}[]{@{}ll@{}}
    \caption{\label{tab:sizing-policy} Options that can be specified within a sizing policy.}\tabularnewline
    \toprule
    \begin{minipage}[b]{0.47\columnwidth}\raggedright
    Option\strut
    \end{minipage} & \begin{minipage}[b]{0.47\columnwidth}\raggedright
    Description\strut
    \end{minipage}\tabularnewline
    \midrule
    \endfirsthead
    \toprule
    \begin{minipage}[b]{0.47\columnwidth}\raggedright
    Option\strut
    \end{minipage} & \begin{minipage}[b]{0.47\columnwidth}\raggedright
    Description\strut
    \end{minipage}\tabularnewline
    \midrule
    \endhead
    \begin{minipage}[t]{0.47\columnwidth}\raggedright
    \textbf{defaultWidth}\strut
    \end{minipage} & \begin{minipage}[t]{0.47\columnwidth}\raggedright
    Default widget width in all contexts (browser, viewer, and knitr).\strut
    \end{minipage}\tabularnewline
    \begin{minipage}[t]{0.47\columnwidth}\raggedright
    \textbf{defaultHeight}\strut
    \end{minipage} & \begin{minipage}[t]{0.47\columnwidth}\raggedright
    Similar to \texttt{defaultWidth}, but for heights instead.\strut
    \end{minipage}\tabularnewline
    \begin{minipage}[t]{0.47\columnwidth}\raggedright
    \textbf{padding}\strut
    \end{minipage} & \begin{minipage}[t]{0.47\columnwidth}\raggedright
    The padding (in pixels) in all contexts.\strut
    \end{minipage}\tabularnewline
    \begin{minipage}[t]{0.47\columnwidth}\raggedright
    \textbf{viewer.defaultWidth}\strut
    \end{minipage} & \begin{minipage}[t]{0.47\columnwidth}\raggedright
    Default widget width within the RStudio Viewer.\strut
    \end{minipage}\tabularnewline
    \begin{minipage}[t]{0.47\columnwidth}\raggedright
    \textbf{viewer.defaultHeight}\strut
    \end{minipage} & \begin{minipage}[t]{0.47\columnwidth}\raggedright
    Similar to \texttt{viewer.defaultWidth}.\strut
    \end{minipage}\tabularnewline
    \begin{minipage}[t]{0.47\columnwidth}\raggedright
    \textbf{viewer.padding}\strut
    \end{minipage} & \begin{minipage}[t]{0.47\columnwidth}\raggedright
    Padding around the widget in the RStudio Viewer (defaults to 15 pixels).\strut
    \end{minipage}\tabularnewline
    \begin{minipage}[t]{0.47\columnwidth}\raggedright
    \textbf{viewer.fill}\strut
    \end{minipage} & \begin{minipage}[t]{0.47\columnwidth}\raggedright
    When displayed in the RStudio Viewer, automatically size the widget to the viewer dimensions. Default to \texttt{TRUE}.\strut
    \end{minipage}\tabularnewline
    \begin{minipage}[t]{0.47\columnwidth}\raggedright
    \textbf{viewer.suppress}\strut
    \end{minipage} & \begin{minipage}[t]{0.47\columnwidth}\raggedright
    Never display the widget within the RStudio Viewer (useful for widgets that require a large amount of space for rendering). Defaults to \texttt{FALSE}.\strut
    \end{minipage}\tabularnewline
    \begin{minipage}[t]{0.47\columnwidth}\raggedright
    \textbf{viewer.paneHeight}\strut
    \end{minipage} & \begin{minipage}[t]{0.47\columnwidth}\raggedright
    Request that the RStudio Viewer be forced to a specific height when displaying this widget.\strut
    \end{minipage}\tabularnewline
    \begin{minipage}[t]{0.47\columnwidth}\raggedright
    \textbf{browser.defaultWidth}\strut
    \end{minipage} & \begin{minipage}[t]{0.47\columnwidth}\raggedright
    Default widget width within a standalone web browser.\strut
    \end{minipage}\tabularnewline
    \begin{minipage}[t]{0.47\columnwidth}\raggedright
    \textbf{browser.defaultHeight}\strut
    \end{minipage} & \begin{minipage}[t]{0.47\columnwidth}\raggedright
    Similar to \texttt{browser.defaultWidth}.\strut
    \end{minipage}\tabularnewline
    \begin{minipage}[t]{0.47\columnwidth}\raggedright
    \textbf{browser.padding}\strut
    \end{minipage} & \begin{minipage}[t]{0.47\columnwidth}\raggedright
    Padding in a standalone browser (defaults to 40 pixels).\strut
    \end{minipage}\tabularnewline
    \begin{minipage}[t]{0.47\columnwidth}\raggedright
    \textbf{browser.fill}\strut
    \end{minipage} & \begin{minipage}[t]{0.47\columnwidth}\raggedright
    When displayed in a standalone web browser, automatically size the widget to the browser dimensions. Defaults to \texttt{FALSE}.\strut
    \end{minipage}\tabularnewline
    \begin{minipage}[t]{0.47\columnwidth}\raggedright
    \textbf{browser.external}\strut
    \end{minipage} & \begin{minipage}[t]{0.47\columnwidth}\raggedright
    Always use an external browser (via \texttt{browseURL()}). Defaults to \texttt{FALSE}, which will result in the use of an internal browser within RStudio v1.1 and higher.\strut
    \end{minipage}\tabularnewline
    \begin{minipage}[t]{0.47\columnwidth}\raggedright
    \textbf{knitr.defaultWidth}\strut
    \end{minipage} & \begin{minipage}[t]{0.47\columnwidth}\raggedright
    Default widget width within documents generated by \textbf{knitr} (e.g., R Markdown).\strut
    \end{minipage}\tabularnewline
    \begin{minipage}[t]{0.47\columnwidth}\raggedright
    \textbf{knitr.defaultHeight}\strut
    \end{minipage} & \begin{minipage}[t]{0.47\columnwidth}\raggedright
    Similar to \texttt{knitr.defaultWidth}.\strut
    \end{minipage}\tabularnewline
    \begin{minipage}[t]{0.47\columnwidth}\raggedright
    \textbf{knitr.figure}\strut
    \end{minipage} & \begin{minipage}[t]{0.47\columnwidth}\raggedright
    Apply the default \textbf{knitr} \texttt{fig.width} and \texttt{fig.height} to the widget rendered in R Markdown. Defaults to \texttt{TRUE}.\strut
    \end{minipage}\tabularnewline
    \bottomrule
    \end{longtable}

    表格转化后

    \begin{table}
    \caption{\label{tab:sizing-policy} Options that can be specified within a sizing policy.}\tabularnewline
    \begin{tabular}{ll}
    \toprule
    \begin{minipage}[b]{0.33\columnwidth}\raggedright
    Option\strut
    \end{minipage} & \begin{minipage}[b]{0.65\columnwidth}\raggedright
    Description\strut
    \end{minipage}\tabularnewline
    \midrule
    
    \begin{minipage}[t]{0.33\columnwidth}\raggedright
    \textbf{defaultWidth}\strut
    \end{minipage} & \begin{minipage}[t]{0.65\columnwidth}\raggedright
    Default widget width in all contexts (browser, viewer, and knitr).\strut
    \end{minipage}\tabularnewline
    \begin{minipage}[t]{0.33\columnwidth}\raggedright
    \textbf{defaultHeight}\strut
    \end{minipage} & \begin{minipage}[t]{0.65\columnwidth}\raggedright
    Similar to \texttt{defaultWidth}, but for heights instead.\strut
    \end{minipage}\tabularnewline
    \begin{minipage}[t]{0.33\columnwidth}\raggedright
    \textbf{padding}\strut
    \end{minipage} & \begin{minipage}[t]{0.65\columnwidth}\raggedright
    The padding (in pixels) in all contexts.\strut
    \end{minipage}\tabularnewline
    \begin{minipage}[t]{0.33\columnwidth}\raggedright
    \textbf{viewer.defaultWidth}\strut
    \end{minipage} & \begin{minipage}[t]{0.65\columnwidth}\raggedright
    Default widget width within the RStudio Viewer.\strut
    \end{minipage}\tabularnewline
    \begin{minipage}[t]{0.33\columnwidth}\raggedright
    \textbf{viewer.defaultHeight}\strut
    \end{minipage} & \begin{minipage}[t]{0.65\columnwidth}\raggedright
    Similar to \texttt{viewer.defaultWidth}.\strut
    \end{minipage}\tabularnewline
    \begin{minipage}[t]{0.33\columnwidth}\raggedright
    \textbf{viewer.padding}\strut
    \end{minipage} & \begin{minipage}[t]{0.65\columnwidth}\raggedright
    Padding around the widget in the RStudio Viewer (defaults to 15 pixels).\strut
    \end{minipage}\tabularnewline
    \begin{minipage}[t]{0.33\columnwidth}\raggedright
    \textbf{viewer.fill}\strut
    \end{minipage} & \begin{minipage}[t]{0.65\columnwidth}\raggedright
    When displayed in the RStudio Viewer, automatically size the widget to the viewer dimensions. Default to \texttt{TRUE}.\strut
    \end{minipage}\tabularnewline
    \begin{minipage}[t]{0.33\columnwidth}\raggedright
    \textbf{viewer.suppress}\strut
    \end{minipage} & \begin{minipage}[t]{0.65\columnwidth}\raggedright
    Never display the widget within the RStudio Viewer (useful for widgets that require a large amount of space for rendering). Defaults to \texttt{FALSE}.\strut
    \end{minipage}\tabularnewline
    \begin{minipage}[t]{0.33\columnwidth}\raggedright
    \textbf{viewer.paneHeight}\strut
    \end{minipage} & \begin{minipage}[t]{0.65\columnwidth}\raggedright
    Request that the RStudio Viewer be forced to a specific height when displaying this widget.\strut
    \end{minipage}\tabularnewline
    \begin{minipage}[t]{0.33\columnwidth}\raggedright
    \textbf{browser.defaultWidth}\strut
    \end{minipage} & \begin{minipage}[t]{0.65\columnwidth}\raggedright
    Default widget width within a standalone web browser.\strut
    \end{minipage}\tabularnewline
    \begin{minipage}[t]{0.33\columnwidth}\raggedright
    \textbf{browser.defaultHeight}\strut
    \end{minipage} & \begin{minipage}[t]{0.65\columnwidth}\raggedright
    Similar to \texttt{browser.defaultWidth}.\strut
    \end{minipage}\tabularnewline
    \begin{minipage}[t]{0.33\columnwidth}\raggedright
    \textbf{browser.padding}\strut
    \end{minipage} & \begin{minipage}[t]{0.65\columnwidth}\raggedright
    Padding in a standalone browser (defaults to 40 pixels).\strut
    \end{minipage}\tabularnewline
    \begin{minipage}[t]{0.33\columnwidth}\raggedright
    \textbf{browser.fill}\strut
    \end{minipage} & \begin{minipage}[t]{0.65\columnwidth}\raggedright
    When displayed in a standalone web browser, automatically size the widget to the browser dimensions. Defaults to \texttt{FALSE}.\strut
    \end{minipage}\tabularnewline
    \begin{minipage}[t]{0.33\columnwidth}\raggedright
    \textbf{browser.external}\strut
    \end{minipage} & \begin{minipage}[t]{0.65\columnwidth}\raggedright
    Always use an external browser (via \texttt{browseURL()}). Defaults to \texttt{FALSE}, which will result in the use of an internal browser within RStudio v1.1 and higher.\strut
    \end{minipage}\tabularnewline
    \begin{minipage}[t]{0.33\columnwidth}\raggedright
    \textbf{knitr.defaultWidth}\strut
    \end{minipage} & \begin{minipage}[t]{0.65\columnwidth}\raggedright
    Default widget width within documents generated by \textbf{knitr} (e.g., R Markdown).\strut
    \end{minipage}\tabularnewline
    \begin{minipage}[t]{0.33\columnwidth}\raggedright
    \textbf{knitr.defaultHeight}\strut
    \end{minipage} & \begin{minipage}[t]{0.65\columnwidth}\raggedright
    Similar to \texttt{knitr.defaultWidth}.\strut
    \end{minipage}\tabularnewline
    \begin{minipage}[t]{0.33\columnwidth}\raggedright
    \textbf{knitr.figure}\strut
    \end{minipage} & \begin{minipage}[t]{0.65\columnwidth}\raggedright
    Apply the default \textbf{knitr} \texttt{fig.width} and \texttt{fig.height} to the widget rendered in R Markdown. Defaults to \texttt{TRUE}.\strut
    \end{minipage}\tabularnewline
    \bottomrule
    \end{tabular}
    \end{table}

    就其中一段判断是否有 Table 16.1 的正则 0.47\\\\columnwidth 来说,在 https://regex101.com/ 中输入

    yihui's hacked table

    以后不管是什么环境(图,表),都可以照这个路子,仿写谢大这段代码

      Cloud2016 我这个例子可以说是暗黑得伸手不见五指,居然还有人看,而且居然还看懂了(再过几个月恐怕我自己都要看不懂了)。我泪流满面。

        个人经验来说用了Rmarkdown应该不用碰latex命令才对…如果用了那就是心不诚

        而且要在R code chunks里插入latex的话 那跑得太偏了…

          yihui 主要是之前啃了一遍正则,?regex 实实在在看了好多遍,还亲自动手逐行翻译,不然我也看不懂,这里面最吓人的就是正则,我建议如果需要根据字符串字面意思匹配的,还是启用 fixed=TRUE 吧!

          xiaoyao177147 正则表达式还加入了反向引用这样的高级用法,比如

          x = gsub('(\\\\FunctionTok\\{[^:]+:)(})(\\\\AttributeTok\\{)(:[^:]+:)', '\\1\\4\\2\\3', x)

          匹配规则的表达式 (\\\\FunctionTok\\{[^:]+:)(})(\\\\AttributeTok\\{)(:[^:]+:) 用圆括号分成了4组, \\1\\4\\2\\3 是替换规则,\\2 表示引用前面的第二个括号内的表达式,所以 \\1\\4\\2\\3 其实是 (\\\\FunctionTok\\{[^:]+:)(:[^:]+:)(})(\\\\AttributeTok\\{)

          The backreference \N, where N = 1 ... 9, matches the substring previously matched by the Nth parenthesized subexpression of the regular expression. (This is an extension for extended regular expressions: POSIX defines them only for basic ones.)

          xiaoyao177147 只要琢磨输入、输出与正则表达式的关系,一行一行地看表达式,试一试就知道表达式是什么意思了,余下的都是正则表达式的基础用法,就不一一详述了

          tctcab 也不一定,举个非用不可例子,目前 Pandoc 就只能将参考文献放在末尾,比如什么学位论文,需要把参考文献放在附录之前,就只好手动插 LaTeX 代码

          <https://github.com/XiangyunHuang/Thesis-Template-Bookdown/blob/master/06-summary.Rmd#L17-L20>

          或者这样

          `r if (knitr::is_latex_output()) '<!--'` 
          \setlength{\bibsep}{0ex}
          \bibliography{latex/refer.bib,latex/book.bib}
          `r if (knitr::is_latex_output()) '-->'`

          欢迎贡献更好的办法

          对 latex 了解稍微粗浅,在写专利的时候经常要遇到到 写伪代码和矩阵的情况,
          默认的 R mark down 貌似支持的不是太好,这块就经常还是用 .tex 文件编辑,不知道各位大神有没有什么好的建议。

            Cloud2016 真是遇到大神了,我是海洋专业的,学了几年r,latex刚接触,你的回复待我慢慢消化。

            第四点,这不是具体的问题,只是个例子。

              7 个月 后