1

I'm trying to create essentially a "new command" or "new environment" with a tabular environment. Is there any possibility to make a simple command that allows me to enter the size of the table and its corresponding entries. For instance, I want to make a command that makes the first table and just change a few numbers to create the second table. How would I accomplish this? Please let me know and thank you.

\begin{document} 
\[
    \begin{tabular}{>{$}c<{$}|*{8}{>{$}c<{$}}}
    & E   & 2A   & B & 2C & 2D\\
    \hline
    \vrule height 12pt width 0pt
    \Gamma_1  &  (1)  &  (1)  & (1) & (1) & (1)\\
    \Gamma_2   & 1  & (1) & 1  & -1 &-1 \\
    \Gamma_3 & 1 & -1   & 1 & 1 & -1\\
    \Gamma_4   & 1   & -1  & 1  & (-1) & 1\\
    \Gamma_5 & (2) & 0 & (-2) & 0 & 0\\
    \end{tabular}
\]
\[
    \begin{tabular}{>{$}l<{$}|*{8}{>{$}l<{$}}}
    C_{3\si{v}}   & E   & C_3   & C_3^2 & \sv & \sv' & \sv'' \\
    \hline
    \vrule height 12pt width 0pt
    E  & E   & C_3   & C_3^2 & \sv & \sv' & \sv'' \\
    C_3   & C_3   & C_3^2 & E  & \sv'' & \sv & \sv' \\
    C_3^2 & C_3^2 & E   & C_3  & \sv' & \sv'' & \sv\\
    \sv   & \sv   & \sv'  & \sv''  & E & C_3 & C_3^2\\
    \sv' & \sv' & \sv'' & \sv & C_3^2 & E & C_3\\
    \sv'' & \sv'' & \sv & \sv' & C_3 &C_3^2 & E\\
    \end{tabular}
\]
\end{document}
8
  • Just out of curiosity: How are \si and \sv defined?
    – Mico
    Feb 15, 2019 at 5:32
  • It would also be helpful if you explained what you mean by "just change a few numbers". The first tabular has 5 (not 8) data columns and 5 data rows, whereas the second tabular has 6 each. Are you looking for an algorithmic way to go from a 5x5 to a 6x6 table?
    – Mico
    Feb 15, 2019 at 5:46
  • Oh sorry, \si is from a siunitx package. And the \sv is defined as \sigma_{v}.
    – Dave2343
    Feb 15, 2019 at 6:15
  • To clarify my initial post, I wanted to basically type 5 or 6 or any number to set the size of the table like 5x5 or 6x6. Then input like a list of numbers or letters to fill the table. If that helps.
    – Dave2343
    Feb 15, 2019 at 6:17
  • I suppose it might be useful to create a macro called, say, \Tstrut -- e.g., \newcommand\Tstrut{\vrule height 12pt width 0pt} -- to simplify inputting a "top strut".
    – Mico
    Feb 15, 2019 at 7:09

1 Answer 1

2

Yes, it makes sense to define an environment for structures that are repeated more than twice in a document.

You should use array and not tabular, but that's essentially all. Find a better name than foo. The optional argument is for the alignment in the columns, default c.

\documentclass{article}
\usepackage{amsmath,array}

\newcommand{\sv}{\sigma_{v}}
\newcommand{\rowstrut}{\rule{0pt}{12pt}}

\newenvironment{foo}[2][c]
 {\begin{array}{#1|*{\value{MaxMatrixCols}}{#1}}#2\\\hline\rowstrut}
 {\end{array}}

\begin{document}

\[
\begin{foo}{& E & 2A & B & 2C & 2D}
  \Gamma_1 & (1) & (1) & (1)  & (1)  & (1) \\
  \Gamma_2 & 1   & (1) & 1    & -1   & -1  \\
  \Gamma_3 & 1   & -1  & 1    & 1    & -1  \\
  \Gamma_4 & 1   & -1  & 1    & (-1) & 1   \\
  \Gamma_5 & (2) & 0   & (-2) & 0    & 0   \\
\end{foo}
\]
\[
\begin{foo}[l]{C_{3v} & E & C_3 & C_3^2 & \sv & \sv' & \sv''}
  E     & E     & C_3   & C_3^2 & \sv   & \sv'  & \sv'' \\
  C_3   & C_3   & C_3^2 & E     & \sv'' & \sv   & \sv'  \\
  C_3^2 & C_3^2 & E     & C_3   & \sv'  & \sv'' & \sv   \\
  \sv   & \sv   & \sv'  & \sv'' & E     & C_3   & C_3^2 \\
  \sv'  & \sv'  & \sv'' & \sv   & C_3^2 & E     & C_3   \\
  \sv'' & \sv'' & \sv   & \sv'  & C_3   & C_3^2 & E     \\
\end{foo}
\]

\end{document}

I used MaxMatrixCols that's the same counter used for matrices in amsmath. You can use *{20}{c} as well.

enter image description here

I removed \si{v} as there is no unit with symbol “v” and every other ‘v’ in the table is in math italic.

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.