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?

  • 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 tex.stackexchange.com/questions/75793/… due to different separators. The question overthere has input with & and \\ . Not the case here. – user4686 Jun 12 '18 at 7: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 9: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. – user4686 Jun 12 '18 at 9:58
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

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

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.