2

I have some narrow text and tabular environments that I'd like to center based on the horizontal space they use up in total. However, it's important that when centered, the original alignments between the elements are preserved. So the effect would be more like increasing margins. Naturally, \centering won't work, as it centers every element individually.

The closest I've come is using a minipage and scaling the width by eye. The total width of the page content might vary though. So is there a way to do this kind of centering?

I guess if there's a way to measure the total width that a page takes, it could be fed to minipage. I have very little experience with LaTeX macros and what's generally possible. So any help would be greatly appreciated!


Here's an example.

Situation now

Wanted result


And a minimal code with all cases:

\documentclass[12pt]{article}
\usepackage[margin=1in]{geometry}
\usepackage[utf8]{inputenc}
\usepackage[finnish]{babel}
\usepackage{gensymb}

\setlength{\parskip}{1em}

\begin{document}

Intro

\begin{tabular}{l|l}
    C & C \\
\end{tabular}

Juttu

\begin{tabular}{l|l|l|l}
    C & C & Am & G \\
    C Dm & C & C \quad G G & C \\
    C & C & Am & G \\
    C Dm & C & C \quad G G & C \\
\end{tabular}

\centering
Intro

\begin{tabular}{l|l}
    C & C \\
\end{tabular}

Juttu

\begin{tabular}{l|l|l|l}
    C & C & Am & G \\
    C Dm & C & C \quad G G & C \\
    C & C & Am & G \\
    C Dm & C & C \quad G G & C \\
\end{tabular}

\centering
\begin{minipage}{0.3\linewidth}
    \setlength{\parskip}{1em}

    Intro

    \begin{tabular}{l|l}
        C & C \\
    \end{tabular}

    Juttu

    \begin{tabular}{l|l|l|l}
        C & C & Am & G \\
        C Dm & C & C \quad G G & C \\
        C & C & Am & G \\
        C Dm & C & C \quad G G & C \\
    \end{tabular}
\end{minipage}
\end{document}
  • 1
    Please make a minimal working example (MWE) that illustrates your issue. – leandriis Mar 19 at 21:31
  • 1
    If you can provide the code that produces the above content, one could better assist you with ideas on how to centre the content. – Werner Mar 19 at 21:32
  • @leandriis Added. – Felix Mar 19 at 21:37
  • @Felix: Thanks for adding the example. If I understand you correctly, you basically would like to achieve the result in the third example? – leandriis Mar 19 at 21:39
  • @leandriis Correct, the second one is the attempt using centering, which I'm sure you see is not the proper output. – Felix Mar 19 at 21:41
3

Instead of a minipage you could use a varwidth environment - this will automatically shrink to the necessary width:

\documentclass[12pt]{article}
\usepackage[margin=1in]{geometry}
\usepackage[utf8]{inputenc}
\usepackage[finnish]{babel}
\usepackage{gensymb}

\setlength{\parskip}{1em}

\usepackage{varwidth}

\begin{document}

\centering
\begin{varwidth}{\linewidth}
    \setlength{\parskip}{1em}

    Intro

    \begin{tabular}{l|l}
        C & C \\
    \end{tabular}

    Juttu

    \begin{tabular}{l|l|l|l}
        C & C & Am & G \\
        C Dm & C & C \quad G G & C \\
        C & C & Am & G \\
        C Dm & C & C \quad G G & C \\
    \end{tabular}
\end{varwidth}
\end{document}

enter image description here

  • Well damn, yet again I'm not alone with my problems. Simple. Effective. Thanks! – Felix Mar 19 at 22:09
  • @Felix You're welcome! Thanks to the nice MWE you prepared, this was easy to answer! – user36296 Mar 19 at 22:10
2

I would just set the constructions inside a tabular (that doesn't have any column padding, although that's necessary). The tabular will expand as needed, ultimately representing an unbreakable box that you can centre:

enter image description here

\documentclass{article}

\usepackage[margin=1in]{geometry}

\begin{document}

\centering
\begin{tabular}{@{} l @{}}
  Intro \\
  \\
  \begin{tabular}{ l | l }
    C & C
  \end{tabular} \\
  \\
  Juttu \\
  \\
  \begin{tabular}{ l | l | l | l }
    C & C & Am & G \\
    C Dm & C & C \quad G G & C \\
    C & C & Am & G \\
    C Dm & C & C \quad G G & C
  \end{tabular}
\end{tabular}

\end{document}
  • Having the line breaks everywhere bothers me a bit. Maybe too much. But yeah, didn't think of that, thanks for the answer! – Felix Mar 19 at 21:57
  • Maybe one could also use Intro \\[\parskip] instead of Intro \\ \\ ? – leandriis Mar 19 at 22:00

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.