3

I use tabular environment in LaTeX.

I want to write a column in a table as a row in the LaTeX code.

For the problem I am writing in LaTeX, it is natural to think of a column at a time.

For example instead of

1 & 3 & 5

2 & 4 & 6

I want to write

1,2 

3,4

5,6 

Is there some code that I can write for this?

CarLaTeX
  • 62,716
emmy
  • 131
  • The memoir class provides this kind of capability for simple tabulars. See section Automatic tabulars in the manual (> texdoc memoir). More information if I was permitted to give an answer. – Peter Wilson Jun 11 '18 at 18:26
  • 6
    @PeterWilson What do you mean "If I was permitted to give an answer?" It seems like a perfectly good answer to me. Even if the OP isn't using memoir having an example from memoir showing how it can be done would be very helpful for others. – Alan Munn Jun 11 '18 at 18:49
  • Not a duplicate of https://tex.stackexchange.com/questions/75793/tabular-input-by-columns-i-e-transpose-a-table due to different separators. The question overthere has input with & and \\. Not the case here. –  Jun 12 '18 at 07:44
  • @jfbu I'm not sure that the separator in and of itself means the questions are different: that would mean every possible marker needs a new question. Can't the other question be answered with a 'flexible' approach which uses a user-definable token? – Joseph Wright Jun 12 '18 at 09:31
  • @JosephWright the other question already has a pgfplotstable answer which seems quite alike CarLaTeX's answer here. I have not read in detail so I don't know if OP will benefit as much (immediately) as from CarLaTeX's answer here. But if this is marked as duplicate, then the green tick awarded to David's \valign answer should be moved to percusse's answer. Marking this as duplicate extends the scope of original question and makes the green tick and also all the previous upvotes obsoleted and in need of complete revision. –  Jun 12 '18 at 09:58
  • @jfbu I think one for a meta question: to me the two questions look similar. (The green tick is the answer that helped the original asker the most, but doens't have to be the best or most general answer at all.) – Joseph Wright Jun 12 '18 at 10:00
  • @JosephWright agreed this raises a general issue. The duplicate mechanism is for helping OP to get good answer. But in case at hand only 1 of the 4 answers of the original can help, so doesn't OP lose time? and it is sort of luck that original had the appropriate answer. –  Jun 12 '18 at 10:02
  • @JosephWright oh sorry, I see Steven's answer over there can perhaps be used here too. –  Jun 12 '18 at 10:03
  • @AlanMunn When I came across the question it was marked as DUPLICATE and only comments were available for input but no provision for answers. – Peter Wilson Jun 12 '18 at 18:23
  • @PeterWilson I see. It was not so marked when I made my comment. But now it is closed again. So maybe you could add a memoir answer to tabular input by columns (i.e. transpose a table) which is the linked duplicate. – Alan Munn Jun 12 '18 at 18:25
  • @AlanMunn The memoir answer does not apply to the linked duplicate but does apply to this question. I disagree with all those who think that this question is a duplicate. – Peter Wilson Jun 12 '18 at 18:34
  • @PeterWilson Well I've added my vote to reopen too. – Alan Munn Jun 12 '18 at 18:35
  • It seems to me that CarLaTeX's solution using the pgfplotstable package is more generic than the memoir class-specific solution. I will now be quiet but unhappy about how "duplicate" works. – Peter Wilson Jun 12 '18 at 19:20

2 Answers2

3

A possible solution is with \pgfplotstabletranspose of pgfplotstable package.

Since you didn't provide a minimal working example (MWE), I did exactly what you asked, but, of course, also the column names can be treated, and the package has many other features to set the column types.

\documentclass{article}
\usepackage{pgfplotstable}
\pgfplotsset{compat=1.16}

\begin{document}
    \pgfplotstableread[col sep=comma]{
        1,2
        3,4
        5,6
        }{\mytabletotranspose}

    \pgfplotstabletranspose{\mytable}{\mytabletotranspose}

    \pgfplotstabletypeset[
        columns={0,1,2},
        every head row/.style={output empty row},
        string type
        ]{\mytable}
\end{document}

enter image description here

CarLaTeX
  • 62,716
2

For the fun of it:

\documentclass{article}

\usepackage{array}
\usepackage{xinttools}

\makeatletter
\newcommand\TrTable[1]{\begin{tabular}{*{10}{c}}\Tr@Table #1\par\par
                       \end{tabular}}

\def\Tr@Table #1\par{\if\relax\detokenize{#1}\relax\else
    \begin{tabular}{@{}c@{}}
    \xintListWithSep{\\}{\xintCSVtoListNoExpand{#1}}
    \end{tabular}\expandafter\Tr@@Table\fi}

% some complication with inserting the &'s only if and when needed
\def\Tr@@Table #1\par{\if\relax\detokenize{#1}\relax\else
    \@firstofone{&}\begin{tabular}{@{}c@{}}
    \xintListWithSep{\\}{\xintCSVtoListNoExpand{#1}}
    \end{tabular}\expandafter\Tr@@Table\fi}

\makeatother

\begin{document}
\TrTable{
1,2 

3,4

5,6 
}

For comparison:

\begin{tabular}{*{3}{c}}
  1&3&5 \\
  2&4&6
\end{tabular}
\end{document}

enter image description here