Blocksatz (rechts/linksbündig, zentriert) für alle Tabellen in einem Dokument

Blocksatz (rechts/linksbündig, zentriert) für alle Tabellen in einem Dokument

Angenommen, ich möchte alle meine Tabellen an einer bestimmten vertikalen Linie (MWE) linksbündig ausrichten:

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

Diese Lösung funktioniert, aber ich möchte mehr Flexibilität. Wie kann ich beispielsweise neu definieren, \leftpaddingdass alle Tabellen auf einmal zentriert werden? Das sollte zumindest in zwei Durchgängen möglich sein. Die Option der Rechtsausrichtung würde auch nicht schaden. Völlig andere Lösungen für denselben Zweck sind ebenfalls willkommen.

Antwort1

Obwohl ich nicht ganz verstehe, was du willst, gibt es hier eine Lösung (die hoffentlich deinen Bedürfnissen entspricht), die das pgfkeys-Paket verwendet. Ich habe die Beschriftungen so gestaltet, dass sie beschriftet werden können (mit der label=Taste), aber den Befehl intern nicht verwenden \caption. Außerdem wird nicht dein Befehl verwendet, \leftpaddingsondern die padding-Taste.

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

BEARBEITEN: Beachten Sie, dass ich den obigen Code leicht geändert habe.

Die Verwendung der placedtabular-Umgebung ist wie üblich tabular, außer dass Sie im optionalen Argument eine durch Kommas getrennte key=valueSyntax verwenden können. Die folgenden Schlüssel sind verfügbar:

  • stretch: der Wert fürarraystretch
  • place: die Platzierung der Tabelle. Verfügbar sind left, rightundmiddle
  • padding: der Abstand zum linken bzw. rechten Rand mit place=leftbzw.place=right
  • caption: die für die Tabelle verwendete Überschrift
  • scaption: der Tabellenname für \listoftables, wenn nicht verwendet, captionwird verwendet. Wenn captionnicht verwendet, hat dies keine Auswirkung.
  • label: die Bezeichnung, die zur Referenzierung der Tabelle verwendet wird

Wenn Sie einen dieser Schlüssel in einer placedtabular-Umgebung verwenden, sind die Auswirkungen nur lokal und wirken sich nicht auf andere Tabellen aus, die mit dieser Umgebung festgelegt wurden.

Von den oben genannten Schlüsseln placehaben stretchund paddingeinen Standardwert, der in Klammern gesetzt wird und default/.style={...}im ersten Aufruf von im obigen MWE verwendet wird \pgfkeys. Dort gibt es auch den Kommentar „Ändern Sie hier die Standardeinstellungen“.

EDIT2: Der folgende Code ist alles zwischen \makeatletterund \makeatotherund setzt nicht 'Tabelle xy' vor die Überschrift. Aus diesem Grund habe ich auch den label-Schlüssel entfernt, da die Referenzierung damit nicht funktioniert.

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

\anderes machen

EDIT3: Vollständiger Code (ohne labelund ohne Table x.y:Präfix), der zwei weitere Optionen enthält:

  • fontwerden Fontswitches auf die Tabelle angewendet (Dinge wie \bfseries, \footnotesizeoder \small)
  • captionfontwerden Fontswitches auf die Überschrift angewendet

Bei beiden ist die Vorgabe, dass die umgebenden Fontswitches nicht geändert werden. Bei Verwendung werden Größe und Schriftart zurückgesetzt und das angegebene Argument angewendet.

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

verwandte Informationen