[英]How to center LaTeX xtable (And Figure) output in full text width
这是我先前发布的一个问题的后续文章( 如何将LaTeX xtable输出以全文宽度居中 )。
我意识到上一篇文章的MWE不完整。 为了尽量减少示例,我确实省略了一些最终导致冲突的内容。 因此,在这里,我将更全面地发布该问题。
我正在使用tufte-handout( http://mirrors.ibiblio.org/CTAN/macros/latex/contrib/tufte-latex/sample-handout.pdf )在乳胶中创建一个小报告。 我有一个文件code.Rnw,我将其编入code.tex。 下面是我的代码。Rnw:
\documentclass[12pt,english,nohyper]{tufte-handout}
\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}
\usepackage{longtable}
\usepackage{wrapfig}
\usepackage{hyperref}
\usepackage{graphicx}
\usepackage[space]{grffile}
\usepackage{geometry}
\usepackage{pgffor}
\usepackage{calc}
\usepackage{enumitem}
\usepackage{microtype}
\usepackage{tabularx}
%\usepackage{floatrow}
\begin{document}
<<include=FALSE>>=
library(ggplot2)
library(xtable)
@
\begin{fullwidth}
\makeatletter\setlength\hsize{\@tufte@fullwidth}\makeatother
<<echo=FALSE,results='asis'>>=
x.big <- xtable(mtcars[1:10,1:4], label ='tab:mtcars',caption ='This is the mtcar dataset head.',align = c("rr|lr|r"))
print(x.big, tabular.environment ='longtable', floating = FALSE, include.rownames=FALSE)
@
\end{fullwidth}
<<echo=FALSE,results='asis'>>=
fnameO <<- "plot.pdf"
pdf(paste0("./",fnameO),width=6,height=7)
print(qplot(hp, mpg, data=mtcars, main="Scatterplots of MPG vs. Horsepower", xlab="Horsepower", ylab="Miles per Gallon"))
{dev.off();invisible()}
@
\begin{fullwidth}
\makeatletter\setlength\hsize{\@tufte@fullwidth}\makeatother
\begin{figure}[!ht]
\includegraphics[width=\linewidth]{\Sexpr{fnameO}}
\caption{This is a plot of the mtcars dataset from R. It compares the horsepower with the miles per gallon. It uses the qplot function from ggplot2.}
\label{fig:LearningObj_summary}
\end{figure}
\end{fullwidth}
\end{document}
这是输出:
我希望表格和图形都居中(在整个页面上)。 如上所示,我能够成功地使表格居中(感谢上一篇文章中用户的建议)。
但是,我无法使图形居中显示在整个页面的中央,并且标题下方。 相反,可能由于我使用的文档类(tufte-handout),该图形本身位于非空白区域,其标题位于空白区域。
首先,我取消了代码中的\\ usepackage {floatrow}的注释,试图将图形标题强制在图形下方而不是图形右侧。 这将导致这样的输出(表格和图形都在左侧而不是居中,但图形标题确实在图形下方):
我的问题是:如何将表格和图形(位于标题下方)居中,以便输出看起来更像这样?:
谢谢。
作为粗略的修改,您可以简单地修改MWE的图形环境以使用原始的\\caption
命令:
\begin{fullwidth}
\begin{figure}[!ht]
\makeatletter\setlength\hsize{\@tufte@fullwidth}\setlength\linewidth{\@tufte@fullwidth}\let\caption\@tufte@orig@caption\makeatother
\includegraphics[width=\linewidth]{\Sexpr{fnameO}}
\caption{This is a plot of the mtcars dataset from R. It compares the horsepower with the miles per gallon. It uses the qplot function from ggplot2.}
\label{fig:LearningObj_summary}
\end{figure}
\end{fullwidth}
...或以页面为中心的较小图形:
\begin{fullwidth}
\begin{figure}[!ht]
\makeatletter\setlength\hsize{\@tufte@fullwidth}\setlength\linewidth{\@tufte@fullwidth}\let\caption\@tufte@orig@caption\makeatother
\centering
\includegraphics[width=.4\linewidth]{\Sexpr{fnameO}}
\caption{This is a plot of the mtcars dataset from R.}
\label{fig:LearningObj_summary}
\end{figure}
\end{fullwidth}
如果您发现缺少原始\\caption
命令,并且文档中的浮点数都没有(!)需要使用tufte字幕,则可以使用\\usepackage[labelfont=bf,compatibility=false]{caption}
类的内容覆盖\\caption
命令。 。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.