在 R 中将 LaTeX 与 TikZDevice 结合使用:是否可以将 Tikzfigures 的字体设置为 10,而将文本的字体设置为 12?

在 R 中将 LaTeX 与 TikZDevice 结合使用:是否可以将 Tikzfigures 的字体设置为 10,而将文本的字体设置为 12?

我使用 R 和 ggplot 创建图形,然后将其作为 tikz 代码导入我的 LaTeX 文件。出版商希望我提交他们的模板,该模板基于[12pt]{article}。但是,每当我使用以下代码导出我的 ggplot2-figure 时,都会出现一个小问题。

if (!require("pacman")) install.packages("pacman")
pacman::p_load( 'ggplot2', 'tikzDevice', 'tinytex')



trialfigure <- ggplot(mpg, aes(y = class)) +
  geom_bar(aes(fill = drv)) +
  scale_fill_discrete(labels = c("4" = "really long label")) +
  theme(legend.position = "bottom")

trialfigure
tikz('trialfigure.tex', width = 6.5, height = 4.875, sanitize = TRUE, documentDeclaration = "\\documentclass[12pt]{article}") 
print(trialfigure)
dev.off()    

在 Overleaf 的 LaTex 输出中,我觉得字体太大,可以看作是“较长的彩色图例”文本总是干扰图例颜色。

如果我将文档设置为 10 点,一切都会按预期进行。

我包含 tikz-code 如下:

\documentclass[12pt,authoryear]{article}
\usepackage{tikz}
\usepackage{xcolor}
\usepackage[utf8]{inputenc} % for easy quotation marks ""

%%%%% for the TIKZPictures
\makeatletter
\tikzset{dangerous style/.code={
    \tikzoption{clip}[]{\pgf@relevantforpicturesizefalse}
    \tikzoption{use as bounding box}[]{\pgf@relevantforpicturesizefalse}
    }
}
\makeatother
%%%%% for the TIKZPictures

\usepackage{graphicx}

\begin{document}

\begin{figure}[htbp!]
 
\centering
\resizebox{\textwidth}{!}{

% Created by tikzDevice version 0.12.3.1 on 2023-07-24 15:04:05
% !TEX encoding = UTF-8 Unicode
\begin{tikzpicture}[x=1pt,y=1pt]
\definecolor{fillColor}{RGB}{255,255,255}
\path[use as bounding box,fill=fillColor,fill opacity=0.00] (0,0) rectangle (469.75,352.32);
\begin{scope}
\path[clip] (  0.00,  0.00) rectangle (469.75,352.32);
\definecolor{drawColor}{RGB}{255,255,255}
\definecolor{fillColor}{RGB}{255,255,255}

\path[draw=drawColor,line width= 0.6pt,line join=round,line cap=round,fill=fillColor] (  0.00,  0.00) rectangle (469.75,352.32);
\end{scope}
\begin{scope}
\path[clip] ( 59.42, 67.12) rectangle (464.26,346.82);
\definecolor{fillColor}{gray}{0.92}

\path[fill=fillColor] ( 59.42, 67.12) rectangle (464.26,346.82);
\definecolor{drawColor}{RGB}{255,255,255}

\path[draw=drawColor,line width= 0.3pt,line join=round] (137.18, 67.12) --
    (137.18,346.82);

\path[draw=drawColor,line width= 0.3pt,line join=round] (255.90, 67.12) --
    (255.90,346.82);

\path[draw=drawColor,line width= 0.3pt,line join=round] (374.62, 67.12) --
    (374.62,346.82);

\path[draw=drawColor,line width= 0.6pt,line join=round] ( 59.42, 90.42) --
    (464.26, 90.42);

\path[draw=drawColor,line width= 0.6pt,line join=round] ( 59.42,129.27) --
    (464.26,129.27);

\path[draw=drawColor,line width= 0.6pt,line join=round] ( 59.42,168.12) --
    (464.26,168.12);

\path[draw=drawColor,line width= 0.6pt,line join=round] ( 59.42,206.97) --
    (464.26,206.97);

\path[draw=drawColor,line width= 0.6pt,line join=round] ( 59.42,245.81) --
    (464.26,245.81);

\path[draw=drawColor,line width= 0.6pt,line join=round] ( 59.42,284.66) --
    (464.26,284.66);

\path[draw=drawColor,line width= 0.6pt,line join=round] ( 59.42,323.51) --
    (464.26,323.51);

\path[draw=drawColor,line width= 0.6pt,line join=round] ( 77.82, 67.12) --
    ( 77.82,346.82);

\path[draw=drawColor,line width= 0.6pt,line join=round] (196.54, 67.12) --
    (196.54,346.82);

\path[draw=drawColor,line width= 0.6pt,line join=round] (315.26, 67.12) --
    (315.26,346.82);

\path[draw=drawColor,line width= 0.6pt,line join=round] (433.98, 67.12) --
    (433.98,346.82);
\definecolor{fillColor}{RGB}{248,118,109}

\path[fill=fillColor] (285.58,111.79) rectangle (356.81,146.75);

\path[fill=fillColor] (303.39,150.64) rectangle (321.20,185.60);

\path[fill=fillColor] ( 77.82,228.33) rectangle (273.71,263.29);

\path[fill=fillColor] (261.84,267.18) rectangle (285.58,302.14);

\path[fill=fillColor] (143.12,306.03) rectangle (445.85,340.99);
\definecolor{fillColor}{RGB}{0,186,56}

\path[fill=fillColor] ( 77.82,111.79) rectangle (285.58,146.75);

\path[fill=fillColor] ( 77.82,150.64) rectangle (303.39,185.60);

\path[fill=fillColor] ( 77.82,189.49) rectangle (143.12,224.45);

\path[fill=fillColor] (131.24,267.18) rectangle (261.84,302.14);
\definecolor{fillColor}{RGB}{97,156,255}

\path[fill=fillColor] ( 77.82, 72.94) rectangle (107.50,107.91);

\path[fill=fillColor] ( 77.82,267.18) rectangle (131.24,302.14);

\path[fill=fillColor] ( 77.82,306.03) rectangle (143.12,340.99);
\end{scope}
\begin{scope}
\path[clip] (  0.00,  0.00) rectangle (469.75,352.32);
\definecolor{drawColor}{gray}{0.30}

\node[text=drawColor,anchor=base east,inner sep=0pt, outer sep=0pt, scale=  0.73] at ( 54.47, 87.90) {2seater};

\node[text=drawColor,anchor=base east,inner sep=0pt, outer sep=0pt, scale=  0.73] at ( 54.47,126.75) {compact};

\node[text=drawColor,anchor=base east,inner sep=0pt, outer sep=0pt, scale=  0.73] at ( 54.47,165.59) {midsize};

\node[text=drawColor,anchor=base east,inner sep=0pt, outer sep=0pt, scale=  0.73] at ( 54.47,204.44) {minivan};

\node[text=drawColor,anchor=base east,inner sep=0pt, outer sep=0pt, scale=  0.73] at ( 54.47,243.29) {pickup};

\node[text=drawColor,anchor=base east,inner sep=0pt, outer sep=0pt, scale=  0.73] at ( 54.47,282.14) {subcompact};

\node[text=drawColor,anchor=base east,inner sep=0pt, outer sep=0pt, scale=  0.73] at ( 54.47,320.98) {suv};
\end{scope}
\begin{scope}
\path[clip] (  0.00,  0.00) rectangle (469.75,352.32);
\definecolor{drawColor}{gray}{0.20}

\path[draw=drawColor,line width= 0.6pt,line join=round] ( 56.67, 90.42) --
    ( 59.42, 90.42);

\path[draw=drawColor,line width= 0.6pt,line join=round] ( 56.67,129.27) --
    ( 59.42,129.27);

\path[draw=drawColor,line width= 0.6pt,line join=round] ( 56.67,168.12) --
    ( 59.42,168.12);

\path[draw=drawColor,line width= 0.6pt,line join=round] ( 56.67,206.97) --
    ( 59.42,206.97);

\path[draw=drawColor,line width= 0.6pt,line join=round] ( 56.67,245.81) --
    ( 59.42,245.81);

\path[draw=drawColor,line width= 0.6pt,line join=round] ( 56.67,284.66) --
    ( 59.42,284.66);

\path[draw=drawColor,line width= 0.6pt,line join=round] ( 56.67,323.51) --
    ( 59.42,323.51);
\end{scope}
\begin{scope}
\path[clip] (  0.00,  0.00) rectangle (469.75,352.32);
\definecolor{drawColor}{gray}{0.20}

\path[draw=drawColor,line width= 0.6pt,line join=round] ( 77.82, 64.37) --
    ( 77.82, 67.12);

\path[draw=drawColor,line width= 0.6pt,line join=round] (196.54, 64.37) --
    (196.54, 67.12);

\path[draw=drawColor,line width= 0.6pt,line join=round] (315.26, 64.37) --
    (315.26, 67.12);

\path[draw=drawColor,line width= 0.6pt,line join=round] (433.98, 64.37) --
    (433.98, 67.12);
\end{scope}
\begin{scope}
\path[clip] (  0.00,  0.00) rectangle (469.75,352.32);
\definecolor{drawColor}{gray}{0.30}

\node[text=drawColor,anchor=base,inner sep=0pt, outer sep=0pt, scale=  0.73] at ( 77.82, 57.12) {0};

\node[text=drawColor,anchor=base,inner sep=0pt, outer sep=0pt, scale=  0.73] at (196.54, 57.12) {20};

\node[text=drawColor,anchor=base,inner sep=0pt, outer sep=0pt, scale=  0.73] at (315.26, 57.12) {40};

\node[text=drawColor,anchor=base,inner sep=0pt, outer sep=0pt, scale=  0.73] at (433.98, 57.12) {60};
\end{scope}
\begin{scope}
\path[clip] (  0.00,  0.00) rectangle (469.75,352.32);
\definecolor{drawColor}{RGB}{0,0,0}

\node[text=drawColor,anchor=base,inner sep=0pt, outer sep=0pt, scale=  0.92] at (261.84, 46.63) {count};
\end{scope}
\begin{scope}
\path[clip] (  0.00,  0.00) rectangle (469.75,352.32);
\definecolor{drawColor}{RGB}{0,0,0}

\node[text=drawColor,rotate= 90.00,anchor=base,inner sep=0pt, outer sep=0pt, scale=  0.92] at ( 11.81,206.97) {class};
\end{scope}
\begin{scope}
\path[clip] (  0.00,  0.00) rectangle (469.75,352.32);
\definecolor{fillColor}{RGB}{255,255,255}

\path[fill=fillColor] (178.83,  5.50) rectangle (344.84, 33.84);
\end{scope}
\begin{scope}
\path[clip] (  0.00,  0.00) rectangle (469.75,352.32);
\definecolor{drawColor}{RGB}{0,0,0}

\node[text=drawColor,anchor=base west,inner sep=0pt, outer sep=0pt, scale=  0.92] at (184.33, 16.52) {drv};
\end{scope}
\begin{scope}
\path[clip] (  0.00,  0.00) rectangle (469.75,352.32);
\definecolor{fillColor}{gray}{0.95}

\path[fill=fillColor] (203.35, 11.00) rectangle (220.70, 28.34);
\end{scope}
\begin{scope}
\path[clip] (  0.00,  0.00) rectangle (469.75,352.32);
\definecolor{fillColor}{RGB}{248,118,109}

\path[fill=fillColor] (204.06, 11.71) rectangle (219.99, 27.63);
\end{scope}
\begin{scope}
\path[clip] (  0.00,  0.00) rectangle (469.75,352.32);
\definecolor{fillColor}{gray}{0.95}

\path[fill=fillColor] (283.04, 11.00) rectangle (300.38, 28.34);
\end{scope}
\begin{scope}
\path[clip] (  0.00,  0.00) rectangle (469.75,352.32);
\definecolor{fillColor}{RGB}{0,186,56}

\path[fill=fillColor] (283.75, 11.71) rectangle (299.67, 27.63);
\end{scope}
\begin{scope}
\path[clip] (  0.00,  0.00) rectangle (469.75,352.32);
\definecolor{fillColor}{gray}{0.95}

\path[fill=fillColor] (313.62, 11.00) rectangle (330.97, 28.34);
\end{scope}
\begin{scope}
\path[clip] (  0.00,  0.00) rectangle (469.75,352.32);
\definecolor{fillColor}{RGB}{97,156,255}

\path[fill=fillColor] (314.33, 11.71) rectangle (330.26, 27.63);
\end{scope}
\begin{scope}
\path[clip] (  0.00,  0.00) rectangle (469.75,352.32);
\definecolor{drawColor}{RGB}{0,0,0}

\node[text=drawColor,anchor=base west,inner sep=0pt, outer sep=0pt, scale=  0.73] at (226.20, 17.15) {really long label};
\end{scope}
\begin{scope}
\path[clip] (  0.00,  0.00) rectangle (469.75,352.32);
\definecolor{drawColor}{RGB}{0,0,0}

\node[text=drawColor,anchor=base west,inner sep=0pt, outer sep=0pt, scale=  0.73] at (305.88, 17.15) {f};
\end{scope}
\begin{scope}
\path[clip] (  0.00,  0.00) rectangle (469.75,352.32);
\definecolor{drawColor}{RGB}{0,0,0}

\node[text=drawColor,anchor=base west,inner sep=0pt, outer sep=0pt, scale=  0.73] at (336.47, 17.15) {r};
\end{scope}
\end{tikzpicture}



 }
\caption{Some caption. The problem is in the "really long label".}
 \label{fig:someplotlabel}
\end{figure}


\end{document}

图形的问题在于大标签。我不想更改 tikz 代码,拜托。

我不确定我能做些什么来获得一个可重现的示例,欢迎提供任何提示。

有没有什么办法可以解决我的 12 pt 字体的问题? - 例如,定义所有数字都应该是 10 pt?

答案1

编辑

(完全重写原始答案,因为问题显示的代码可以更好地理解问题。)

A) 首先,您的 R 代码有问题。您似乎创建了一个“文档无关”的 tikz 图片,但没有使用选项standAlone=TRUE,所以我猜这tikzDocumentDeclaration没有效果,并且该图形是按默认值(10pt)计算的。12pt latex 文本中用于 10pt 的 tikz 图片可能会产生此标签溢出。

但即使制作一个添加此选项的独立 TeX 文档,tkiz 的字体指标调整似乎也会以这种方式失败(下面的 R 脚本,在 12pt 文档中制作了错误的图片,的输出trialfigure.tex包含在下图 1 中MWE.Rnw):

剧本MWE.R

require(ggplot2)
require(tikzDevice)

options(tikzDocumentDeclaration = "\\documentclass[12pt]{article}")
    
trialfigure <- ggplot(mpg, aes(y = class)) +
  geom_bar(aes(fill = drv)) +
  scale_fill_discrete(labels = c("4" = "really long label")) +
  theme(legend.position = "bottom")

trialfigure
tikz('trialfigure.tex', 
     width = 6.5, height = 4.875, 
     standAlone=TRUE,
     sanitize = TRUE) 
print(trialfigure)
dev.off()    

现在你有几种解决这个问题的方法:

  1. 不要问我为什么,但是将源代码中的标签“非常长的标签”更改为“非常长的标签”(只需将“o”替换为“a”)就可以解决这个问题。这似乎是一个错误,无法用这个确切的标签测量 12pt 的正确尺寸,但用 10pt 或 11pt 则不行。我认为使用明显更长的 12 pt 标签作为“非常长的长标签”没有任何问题。但想想其他计算错误的情况:

  2. 大概是,将pointsize=12选项添加到tikz()函数中。如果无法生成具有有效 Latex 字体大小的数学运算,则将其用作基本字体大小tikzDocumentDeclaration。但它不适用于该死的“非常长的标签”。

  3. 将图像设为默认的 10 pt,保存 pdf,然后在最终文档中将图像放大 20%(比例=1.2)。

  4. 编辑trialfigure.tex\begin{tikzpicture}在行中更改 [x=1pt,y=1pt][x=1.2pt,y=1.2pt],并注意tikz()在需要时在函数中按比例减小图片尺寸,以避免在 LaTeX 端调整大小。

  5. 或者,通过 .Rnw 文件制作“几乎”相同的独立文档knitr。现在 R 将正确地为 12 pt 制作图形,因此无需修改或缩放即可使用(图 2)。不知道为什么“非常长的标签”在这里没有问题,但尽管图看起来相同,但所有 tikz 坐标都不同,所以也许这可以避免错误?

B) 其次,正如原始答案中所述,您在使用 LaTeX 时也遇到了问题\resizebox。在 12pt 文档中使用 10pt 的正确 tikz 代码会产生标签溢出(任何标签,不仅仅是这个奇怪的标签),但除此之外,字体大小在图表之间和与正文之间会不一致,具体取决于每个字体是放大还是缩小,非常丑陋。如果您包含 pdf 图像而不是 tikz 代码,则在使用任何类型的重新尺寸、通过 \includegraphics选项或文档自动设置图像尺寸(例如:\setkeys{Gin}{width=3cm,height=3cm})时,您将面临同样的问题。特别要注意,Rstudio 报告在 PDF(通过 LaTeX)中使用模板自动缩小超过\maxdimen(图 3)的大文件。因此,如果最终字体大小很重要,请再次注意 R 端的尺寸,以避免在 LaTeX 端重新尺寸。

尽管如此,请注意,无论您使用 R 脚本还是 knitr 将此图放大或缩小(10pt 或 12pt),tikz 节点对于轴刻度标签总是会缩放到 92% 或 73%。在 12pt 文档中,这意味着未缩放图像的字体大小分别约为 11pt 和 8pt。如果这很重要,您还应该cex在 R 端进行控制。

文件MWE.Rnw

\documentclass[12pt]{article}
\usepackage[bmargin=1cm,tmargin=1cm]{geometry}
\usepackage{tikz}
\parskip1em\parindent0pt
\begin{document}

\begin{figure}[h!]
\centering
\includegraphics[scale=1]{trialfigure.pdf}
\caption{Plot generated by the standalone R script}
\end{figure} 

<<theplot,echo=F,include=F>>=
require(ggplot2)
trialfigure <- ggplot(mpg, aes(y = class)) +
  geom_bar(aes(fill = drv)) +
  scale_fill_discrete(labels = c("4" = "really long label")) +
  theme(legend.position = "bottom")
@
<<herewego,echo=F,dev='tikz',fig.cap="Plot generated with knitr.", fig.align='center', fig.width=4,  fig.height=2,  message=FALSE, fig.pos="h!">>=
trialfigure 
@

<<herewego2,echo=F,dev='tikz',fig.cap="The same but too big = smaller fonts.", fig.align='center', fig.width=6.5,  fig.height=2,  message=FALSE, fig.pos="h!">>=
trialfigure 
@
\end{document}

The MWE.pdf

新mwe

笔记:

是的,knitr工作流程非常不同,但恕我直言,除了很好地处理这个问题之外,还有很多优点。

相关内容