Justificando ('irregular direita/esquerda, centro') todas as tabelas em um documento

Justificando ('irregular direita/esquerda, centro') todas as tabelas em um documento

Suponha que eu queira justificar à esquerda todas as minhas tabelas para uma determinada linha vertical (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}

Esta solução funciona, mas quero mais flexibilidade. Como, por exemplo, redefinirei \leftpaddingpara centralizar todas as mesas ao mesmo tempo? Deveria ser possível, pelo menos em duas passagens. A opção de justificar corretamente também não faria mal. Soluções totalmente diferentes para o mesmo propósito também são bem-vindas.

Responder1

Embora eu não tenha entendido exatamente o que você deseja, aqui está uma solução (que espero que atenda às suas necessidades) usando o pgfkeys-package. Fiz as legendas de uma forma que elas podem ser rotuladas (usando a label=tecla) mas não usam o \captioncomando internamente. Além disso, não usa o seu comando, \leftpaddingmas a 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}

EDIT: Observe que alterei ligeiramente o código acima.

O uso do placedtabular-environment é normal tabular, exceto que no argumento opcional você pode usar uma key=valuesintaxe separada por vírgulas. As seguintes chaves estão disponíveis:

  • stretch: o valor usado paraarraystretch
  • place: o posicionamento da mesa. Estão disponíveis lefterightmiddle
  • padding: o espaço à margem esquerda ou direita com place=leftou place=right, respectivamente
  • caption: a legenda usada para a tabela
  • scaption: o nome da tabela para \listoftables, se não for usado, captionserá usado. Se captionnão for usado, isso não terá nenhum efeito.
  • label: o rótulo usado para referenciar a tabela

Quando você usa uma dessas chaves em um placedtabularambiente, os efeitos são apenas locais e não afetam nenhuma outra tabela definida com esse ambiente.

Das chaves mencionadas acima place, stretche paddingpossuem um valor padrão, que é definido entre colchetes e default/.style={...}é usado dentro da primeira chamada do \pgfkeysMWE acima. Há também o comentário ‘altere as configurações padrão aqui’.

EDIT2: O código abaixo é tudo entre \makeatlettere \makeatothere não define 'tabela xy' na frente da legenda. Por causa disso, também removi a label-key, pois a referência não funcionará com isso.

\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: Código completo (sem labele sem Table x.y:prefixo) que inclui mais duas opções:

  • fontsão fontswitches aplicados à tabela (coisas como \bfseries, \footnotesizeou \small)
  • captionfontsão fontswitches aplicados à legenda

Ambos têm como padrão nenhuma alteração nas opções de fontes adjacentes. Se usado, redefina o tamanho e a fonte e aplique o argumento fornecido.

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

informação relacionada