假设我想将所有表格左对齐到某条垂直线(MWE):
\documentclass[11pt,a4paper,oneside]{memoir}
\pagestyle{plain}
\usepackage{fontspec}
%\usepackage{changepage}
\setmainfont[Mapping=tex-text]{Liberation Serif}
\setsansfont[Mapping=tex-text]{Liberation Sans}
\setmonofont[Mapping=tex-text]{Liberation Mono}
\newcommand{\leftpadding}{1cm}
\begin{document}
\chapter{}
All Gaul is divided into three parts, one of which the Belgae inhabit, the Aquitani another, those who in their own language are called Celts, in our Gauls, the third. All these differ from each other in language, customs and laws. The river Garonne separates the Gauls from the Aquitani; the Marne and the Seine separate them from the Belgae.
\medskip\begin{adjustwidth}{\leftpadding}{0cm}
\raggedright{Caption}\\
\renewcommand*{\arraystretch}{1.4}
\footnotesize\begin{tabular}[c]{|c|c|}
\hline
{Alfa}
& {Bravo}
\\\hline
{Charlie}
& {Delta}
\\\hline
\end{tabular}
\end{adjustwidth}
\bigskip
Of all these, the Belgae are the bravest, because they are furthest from the civilization and refinement of [our] Province, and merchants least frequently resort to them, and import those things which tend to effeminate the mind.
\medskip\begin{adjustwidth}{\leftpadding}{0cm}
\raggedright{Another Caption}\\
\renewcommand*{\arraystretch}{1.4}
\footnotesize\begin{tabular}[c]{|c|c|}
\hline
{Longer Echo}
& {Long Foxtrot}
\\\hline
{Golf}
& {Hotel}
\\\hline
\end{tabular}
\end{adjustwidth}
\bigskip
They are the nearest to the Germans, who dwell beyond the Rhine, with whom they are continually waging war; for which reason the Helvetii also surpass the rest of the Gauls in valor.
\end{document}
这个解决方案可行,但我希望有更大的灵活性。例如,我该如何重新定义\leftpadding
以一次将所有表格居中?这应该是可能的,至少分两次进行。右对齐选项也不会有什么坏处。也欢迎为同一目的采用完全不同的解决方案。
答案1
虽然我不太明白你想要什么,但这里有一个使用 -package 的解决方案(希望它能满足你的需求)pgfkeys
。我以某种方式制作了标题,它们可以被标记(使用键label=
),但它们在内部不使用命令\caption
。此外,它不使用你的命令,\leftpadding
而是padding
使用 -key。
\documentclass[11pt,a4paper,oneside]{memoir}
\pagestyle{plain}
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
%\usepackage{fontspec}
%\usepackage{changepage}
%\setmainfont[Mapping=tex-text]{Liberation Serif}
%\setsansfont[Mapping=tex-text]{Liberation Sans}
%\setmonofont[Mapping=tex-text]{Liberation Mono}
\usepackage{pgfkeys}
\makeatletter
\newif\if@placedleft
\newif\if@placedright
\newif\if@placedmiddle
\newif\if@placedcapused
\newif\if@placedscapused
\newif\if@placedlabused
\newdimen\@placedpadding
\newdimen\@placedrest
\pgfkeys{/placedt/.is family,/placedt,
default/.style={% change the default settings here
place=left,
stretch=1.4,
padding=1cm,
},
stretch/.estore in=\arraystretch,
place/.is choice,
place/left/.code=\@placedlefttrue\@placedrightfalse\@placedmiddlefalse,
place/right/.code=\@placedrighttrue\@placedleftfalse\@placedmiddlefalse,
place/middle/.code=\@placedmiddletrue\@placedleftfalse\@placedrightfalse,
padding/.estore in=\@placedpadding,
caption/.code=\@placedcapusedtrue\def\@placedcap{#1},
scaption/.code=\@placedscapusedtrue\def\@placedscap{#1},
label/.code=\@placedlabusedtrue\def\@placedlab{#1},
}
\newenvironment{placedtabular}[2][]{%
\par\medskip\noindent\begin{minipage}{\textwidth}%
\pgfkeys{/placedt, default, #1}%
\if@placedleft%
\@placedrest=\textwidth%
\advance\@placedrest by -\@placedpadding%
\hskip\@placedpadding%
\begin{minipage}{\@placedrest}%
\raggedright%
\else\if@placedmiddle%
\centering%
\else\if@placedright%
\@placedrest=\textwidth%
\advance\@placedrest by -\@placedpadding%
\begin{minipage}{\@placedrest}%
\raggedleft%
\fi\fi\fi%
\if@placedcapused%
\refstepcounter{table}%
\tablename~\thetable: \@placedcap%
\if@placedscapused%
\addcontentsline{lot}{table}{\@placedscap}%
\else%
\addcontentsline{lot}{table}{\@placedcap}%
\fi%
\if@placedlabused%
\label{\@placedlab}%
\fi%
\\[0.2\baselineskip]%
\fi%
\footnotesize\begin{tabular}{#2}%
}{
\end{tabular}%
\if@placedmiddle\else%
\end{minipage}%
\fi%
\end{minipage}%
}
\makeatother
\newcommand{\leftpadding}{1cm}
\begin{document}
\listoftables
\chapter{}
All Gaul is divided into three parts, one of which the Belgae inhabit, the Aquitani another, those who in their own language are called Celts, in our Gauls, the third. All these differ from each other in language, customs and laws. The river Garonne separates the Gauls from the Aquitani; the Marne and the Seine separate them from the Belgae.
\begin{placedtabular}[caption=Caption,label=tab:first]{|c|c|}
\hline
{Alfa}
& {Bravo}
\\\hline
{Charlie}
& {Delta}
\\\hline
\end{placedtabular}
\medskip\begin{adjustwidth}{\leftpadding}{0cm}
\raggedright{Caption}\\
\renewcommand*{\arraystretch}{1.4}
\footnotesize\begin{tabular}[c]{|c|c|}
\hline
{Alfa}
& {Bravo}
\\\hline
{Charlie}
& {Delta}
\\\hline
\end{tabular}
\end{adjustwidth}
\bigskip
Of all these, the Belgae are the bravest, because they are furthest from the civilization and refinement of [our] Province, and merchants least frequently resort to them, and import those things which tend to effeminate the mind.
\begin{placedtabular}[caption=Another Caption,label=tab:second]{|c|c|}
\hline
{Longer Echo}
& {Long Foxtrot}
\\\hline
{Golf}
& {Hotel}
\\\hline
\end{placedtabular}
\medskip\begin{adjustwidth}{\leftpadding}{0cm}
\raggedright{Another Caption}\\
\renewcommand*{\arraystretch}{1.4}
\footnotesize\begin{tabular}[c]{|c|c|}
\hline
{Longer Echo}
& {Long Foxtrot}
\\\hline
{Golf}
& {Hotel}
\\\hline
\end{tabular}
\end{adjustwidth}
\bigskip
They are the nearest to the Germans, who dwell beyond the Rhine, with whom they are continually waging war; for which reason the Helvetii also surpass the rest of the Gauls in valor.
\end{document}
编辑:请注意,我稍微修改了上面的代码。
-environment的使用placedtabular
与正常情况一样tabular
,除了在可选参数中可以使用逗号分隔key=value
语法。以下键是可用的:
stretch
:用于的值arraystretch
place
:表格的放置位置。可用的有left
、right
和middle
padding
:分别用place=left
或指定左边距或右边距place=right
caption
:表格使用的标题scaption
:的表名\listoftables
,若未使用则caption
使用。caption
若未使用,则无任何效果。label
:用于引用表格的标签
当您在 -environment 中使用这些键之一时placedtabular
,其效果仅限于本地,不会影响此环境中的任何其他表集。
从上述键place
、stretch
和 中,padding
有一个默认值,它们设置在 的括号之间,default/.style={...}
该括号用于\pgfkeys
上述 MWE 中的第一个 调用。还有注释“在此处更改默认设置”。
EDIT2:下面的代码是\makeatletter
和之间的所有内容\makeatother
,并且没有在标题前面设置“table xy”。因此,我还删除了-key,label
因为引用不适用于此。
\makeatletter
\newif\if@placedleft
\newif\if@placedright
\newif\if@placedmiddle
\newif\if@placedcapused
\newif\if@placedscapused
%\newif\if@placedlabused
\newdimen\@placedpadding
\newdimen\@placedrest
\pgfkeys{/placedt/.is family,/placedt,
default/.style={%
place=left,
stretch=1.4,
padding=1cm,
},
stretch/.estore in=\arraystretch,
place/.is choice,
place/left/.code=\@placedlefttrue\@placedrightfalse\@placedmiddlefalse,
place/right/.code=\@placedrighttrue\@placedleftfalse\@placedmiddlefalse,
place/middle/.code=\@placedmiddletrue\@placedleftfalse\@placedrightfalse,
padding/.estore in=\@placedpadding,
caption/.code=\@placedcapusedtrue\def\@placedcap{#1},
scaption/.code=\@placedscapusedtrue\def\@placedscap{#1},
}
\newenvironment{placedtabular}[2][]{%
\par\medskip\noindent\begin{minipage}{\textwidth}%
\pgfkeys{/placedt, default, #1}%
\if@placedleft%
\@placedrest=\textwidth%
\advance\@placedrest by -\@placedpadding%
\hskip\@placedpadding%
\begin{minipage}{\@placedrest}%
\raggedright%
\else\if@placedmiddle%
\centering%
\else\if@placedright%
\@placedrest=\textwidth%
\advance\@placedrest by -\@placedpadding%
\begin{minipage}{\@placedrest}%
\raggedleft%
\fi\fi\fi%
\if@placedcapused%
\@placedcap%
\if@placedscapused%
\addcontentsline{lot}{table}{\@placedscap}%
\else%
\addcontentsline{lot}{table}{\@placedcap}%
\fi%
\\[0.2\baselineskip]%
\fi%
\footnotesize\begin{tabular}{#2}%
}{
\end{tabular}%
\if@placedmiddle\else%
\end{minipage}%
\fi%
\end{minipage}%
}
\makeatother
EDIT3:完整代码(不带label
和不带Table x.y:
前缀),其中包括另外两个选项:
font
是否将字体切换应用于表格(例如\bfseries
、\footnotesize
或\small
)captionfont
字体切换是否应用于标题
两者默认不改变周围的字体开关。如果使用,则重置大小和字体并应用提供的参数。
\documentclass[11pt,a4paper,oneside]{memoir}
\pagestyle{plain}
\usepackage{fontspec}
%\usepackage{changepage}
\setmainfont[Mapping=tex-text]{Liberation Serif}
\setsansfont[Mapping=tex-text]{Liberation Sans}
\setmonofont[Mapping=tex-text]{Liberation Mono}
\usepackage{pgfkeys}
\makeatletter
\newif\if@placedleft
\newif\if@placedright
\newif\if@placedmiddle
\newif\if@placedcapused
\newif\if@placedscapused
\newif\if@placedfontused
\newif\if@placedcfontused
\newdimen\@placedpadding
\newdimen\@placedrest
\pgfkeys{/placedt/.is family,/placedt,
default/.style={%
place=left,
stretch=1.4,
padding=1cm,
},
stretch/.estore in=\arraystretch,
place/.is choice,
place/left/.code=\@placedlefttrue\@placedrightfalse\@placedmiddlefalse,
place/right/.code=\@placedrighttrue\@placedleftfalse\@placedmiddlefalse,
place/middle/.code=\@placedmiddletrue\@placedleftfalse\@placedrightfalse,
padding/.estore in=\@placedpadding,
caption/.code=\@placedcapusedtrue\def\@placedcap{#1},
scaption/.code=\@placedscapusedtrue\def\@placedscap{#1},
font/.store in=\@placedfont,
captionfont/.store in=\@placedcapfont,
font/.code=\@placedfontusedtrue\def\@placedfont{#1},
captionfont/.code=\@placedcfontusedtrue\def\@placedcapfont{#1},
}
\newenvironment{placedtabular}[2][]{%
\par\medskip\noindent\begin{minipage}{\textwidth}%
\pgfkeys{/placedt, default, #1}%
\if@placedleft%
\@placedrest=\textwidth%
\advance\@placedrest by -\@placedpadding%
\hskip\@placedpadding%
\begin{minipage}{\@placedrest}%
\raggedright%
\else\if@placedmiddle%
\centering%
\else\if@placedright%
\@placedrest=\textwidth%
\advance\@placedrest by -\@placedpadding%
\begin{minipage}{\@placedrest}%
\raggedleft%
\fi\fi\fi%
\if@placedcapused%
{\if@placedcfontused\normalsize\normalfont\@placedcapfont\fi\@placedcap}%
\if@placedscapused%
\addcontentsline{lot}{table}{\@placedscap}%
\else%
\addcontentsline{lot}{table}{\@placedcap}%
\fi%
\\[0.2\baselineskip]%
\fi%
\if@placedfontused\normalsize\normalfont\@placedfont\fi%
\begin{tabular}{#2}%
}{
\end{tabular}%
\if@placedmiddle\else%
\end{minipage}%
\fi%
\end{minipage}%
}
\makeatother
\newcommand{\leftpadding}{1cm}
\begin{document}
\listoftables
\chapter{}
All Gaul is divided into three parts, one of which the Belgae inhabit, the Aquitani another, those who in their own language are called Celts, in our Gauls, the third. All these differ from each other in language, customs and laws. The river Garonne separates the Gauls from the Aquitani; the Marne and the Seine separate them from the Belgae.
\begin{placedtabular}[caption=Caption,font=\bfseries]{|c|c|}
\hline
{Alfa}
& {Bravo}
\\\hline
{Charlie}
& {Delta}
\\\hline
\end{placedtabular}
\medskip\begin{adjustwidth}{\leftpadding}{0cm}
\raggedright{Caption}\\
\renewcommand*{\arraystretch}{1.4}
\footnotesize\begin{tabular}[c]{|c|c|}
\hline
{Alfa}
& {Bravo}
\\\hline
{Charlie}
& {Delta}
\\\hline
\end{tabular}
\end{adjustwidth}
\bigskip
Of all these, the Belgae are the bravest, because they are furthest from the civilization and refinement of [our] Province, and merchants least frequently resort to them, and import those things which tend to effeminate the mind.
\begin{placedtabular}[caption={Another\protect\\Caption}]{|c|c|}
\hline
{Longer Echo}
& {Long Foxtrot}
\\\hline
{Golf}
& {Hotel}
\\\hline
\end{placedtabular}
\begin{placedtabular}[caption=Another Caption]{|c|c|}
\hline
{Longer Echo}
& {Long Foxtrot}
\\\hline
{Golf}
& {Hotel}
\\\hline
\end{placedtabular}
\medskip\begin{adjustwidth}{\leftpadding}{0cm}
\raggedright{Another Caption}\\
\renewcommand*{\arraystretch}{1.4}
\footnotesize\begin{tabular}[c]{|c|c|}
\hline
{Longer Echo}
& {Long Foxtrot}
\\\hline
{Golf}
& {Hotel}
\\\hline
\end{tabular}
\end{adjustwidth}
\bigskip
They are the nearest to the Germans, who dwell beyond the Rhine, with whom they are continually waging war; for which reason the Helvetii also surpass the rest of the Gauls in valor.
\end{document}