如何以完整的文本宽度居中放置LaTeX xtable(和图) [英] How to center LaTeX xtable (And Figure) output in full text width

查看:570
本文介绍了如何以完整的文本宽度居中放置LaTeX xtable(和图)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我之前发布的问题的后续内容(

This is a follow-up to a question I posted earlier (How to center LaTeX xtable output in full text width).

我意识到上一篇文章中的MWE不完整.为了尽量减少示例,我确实省略了一些最终导致冲突的内容.因此,在这里,我将更全面地发布该问题.

I realize that my MWE from this previous post was incomplete. In an effort to make it as minimal of an example as possible, I did leave out something that ended up conflicting. Hence, here, I am posting the issue more fully.

我正在使用tufte-handout( http://mirrors.ibiblio.org/CTAN/macros/latex/contrib/tufte-latex/sample-handout.pdf )在乳胶中创建一个小报告.我有一个文件code.Rnw,我将其编入code.tex.下面是我的代码.Rnw:

I am using tufte-handout (http://mirrors.ibiblio.org/CTAN/macros/latex/contrib/tufte-latex/sample-handout.pdf) to create a small report in latex. I have a file code.Rnw that I knit into code.tex. Below is my code.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}

这是输出:

我希望表格和图形都居中(在整个页面上).如上所示,我能够成功地使表格居中(感谢上一篇文章中用户的建议).

I am desiring to have both the table and the figure centered (across the whole page). As shown above, I am successfully able to get the table centered (thanks to advice from a user in my previous post).

但是,我无法使图形居中显示在整个页面的中央,并且标题下方.相反,可能由于我使用的文档类别(tufte-handout),该图形本身位于非空白区域,其标题位于空白区域.

However, I am unable to get the figure centered across the whole page with the caption below it. Instead, likely due to the document class I am using (tufte-handout), the figure itself is in the non-margin area, and its caption is in the margin area.

对于初学者来说,我未注释代码中的\ usepackage {floatrow},以试图将图形标题置于图形下方而不是图形右侧.这将导致这样的输出(表格和图形都在左侧而不是居中,但图形标题确实在图形下方):

For starters, I uncommented the \usepackage{floatrow} in the code, in an attempt to force the figure caption to be below the figure instead of to the right of it. This lead to an output as such (where both the table and figure are undesirably on the left side instead of centered, but the figure caption is indeed below the figure):

我的问题是:如何将表格和图形同时居中(标题下方),以便输出看起来更像这样?:

My question is: How can I center both the table and figure (with the caption below it), so that the output would look more like this?:

谢谢.

推荐答案

作为粗略的修改,您可以简单地修改MWE的图形环境以使用原始的\caption命令:

As a crude hack, you could simply modify the figure environment of your MWE to use the original \caption command:

\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}

...或者,对于位于页面中心的较小数字:

...or, for a bit smaller figure that is centered on the page:

\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命令.

If you find the original \caption command lacking and if none(!) of the floats in your document need to use tufte captions you can overwrite the \caption command using something like \usepackage[labelfont=bf,compatibility=false]{caption}.

这篇关于如何以完整的文本宽度居中放置LaTeX xtable(和图)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆