文書内のすべての表を両端揃え(「右/左揃え、中央揃え」)にする

文書内のすべての表を両端揃え(「右/左揃え、中央揃え」)にする

すべてのテーブルを特定の垂直線 (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すべてのテーブルを一度に中央に配置するように再定義するにはどうすればよいでしょうか。少なくとも 2 回のパスで可能になるはずです。右揃えのオプションも問題はありません。同じ目的に対するまったく異なるソリューションも歓迎します。

答え1

何をしたいのかよくわかりませんが、pgfkeys-package を使用した解決策 (ニーズに合うといいのですが) を次に示します。キャプションは、(キーを使用して) ラベルを付けることができるように作成しましたlabel=が、内部的にはコマンドは使用しません。また、コマンドではなく -keyを\caption使用します。\leftpaddingpadding

\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: テーブルの配置。leftrightおよび が使用可能です。middle
  • paddingplace=left:またはでplace=rightそれぞれ左余白または右余白のスペース
  • caption: 表に使われるキャプション
  • scaption: のテーブル名。\listoftables使用されていない場合はcaptionが使用されます。 がcaption使用されていない場合は、効果はありません。
  • label: テーブルを参照するために使用されるラベル

これらのキーの 1 つを -environment で使用するとplacedtabular、その効果はローカルのみとなり、この環境で設定された他のテーブルには影響しません。

前述のキーplacestretch、にはpaddingデフォルト値があり、これらは の括弧内に設定され、上記の MWEdefault/.style={...}の最初の の呼び出し内において使用されます\pgfkeys。「ここでデフォルト設定を変更してください」というコメントもあります。

\makeatletter編集2: 以下のコードは、との間すべてであり\makeatother、キャプションの前に「table xy」を設定しません。このため、label参照が機能しないため、-keyも削除しました。

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

\他のものを作る

編集3: 完全なコード (labelプレフィックスなしとプレフィックスなしTable x.y:)。さらに 2 つのオプションが含まれています。

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

関連情報