5

I have an \xymatrix with 4 rows and 2 columns. I want to center it horizontally. I used \centerline{} but it (of course) just centers the whole box created by \xymatrix.

Since my matrix entries are textual and one of them is wider that others, centering whole box doesn't seem right. I would like to center it with respect to the center of the matrix rather than with respect to the center of produced box. Is it possible?

Here is an example:

\documentclass{article}
\usepackage[all]{xy}

\begin{document}
    \hrule
    \centerline{\xymatrix{
        \txt{short} \ar[d] \ar[r] & \txt{short} \ar[d] \\
        \txt{long long long long long} \ar[d] \ar[r] & \txt{short} \ar[d] \\
        \txt{short} \ar[r] & \txt{short}
    }}
    \hrule
\end{document}

  • 1
    Welcome to TeX.SX! Please add a minimal working example (MWE) that illustrates your problem. It will be much easier for us to reproduce your situation and find out what the issue is when we see compilable code, starting with \documentclass{...} and ending with \end{document}. – karlkoeller Feb 28 '14 at 18:12
  • What is the center of the matrix for you? For me I understand it as the center of the box created by \xymatrix{}. – Sigur Feb 28 '14 at 19:56
  • @Sigur: By horizontal center of a matrix with two columns I mean the average of centers of the columns. I mean the center of the grid of the matrix when you imagine just points instead of actual contents of the elements. – user87690 Feb 28 '14 at 20:06
  • @user87690, see if this can help you tex.stackexchange.com/a/154662/14757 – Sigur Feb 28 '14 at 20:12
5

I'm afraid this can not be done from within the \xymatrix and requires to make some calculations.

So, we load the calc package and calculate the horizontal spacing that has to be added to achieve what you want:

\usepackage{calc}

\newlength\mylength
\setlength\mylength{(\widthof{long long long long long}-\widthof{short})/2}

Then we add an \hspace*{\mylength} to move the \xymatrix in the desired position.

MWE:

\documentclass{article}
\usepackage[all]{xy}
\usepackage{calc}

\newlength\mylength
\setlength\mylength{(\widthof{long long long long long}-\widthof{short})/2}

\begin{document}
    \hrule
    \centerline{\xymatrix{
        \txt{short} \ar[d] \ar[r] & \txt{short} \ar[d] \\
        \txt{long long long long long} \ar[d] \ar[r] & \txt{short} \ar[d] \\
        \txt{short} \ar[r] & \txt{short}
    }\hspace*{\mylength}}
    \hrule
\end{document} 

Output:

enter image description here

  • Would it be possible to calculate the \mylength in a way like <position of center of the matrix from left of the produce box> * 2 - \widthof{\xymatrix{…}} so we don't rely on particular content of the elements? – user87690 Mar 1 '14 at 9:26
  • @user87690 I'm afraid not. – karlkoeller Mar 1 '14 at 10:12
2
\documentclass{article}
\usepackage[all]{xy}

\begin{document}
    \hrule
    \centerline{\xymatrix{%
        \txt{short} \ar[d] \ar[r] & \txt{short} \ar[d] \\
        \txt{\makebox[2em][r]{long long long long long}} \ar[d] \ar[r] & \txt{short} \ar[d] \\
        \txt{short} \ar[r] & \txt{short}
    }}
    \hrule
\end{document}

enter image description here

  • Straight and Simple. Just use \centerline. Super-liked this one! It successfully horizontally centre-aligned my commutative diagrams! – Karthik C Oct 28 '14 at 1:54

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.