5

I have many tables like this:

\begin{table}[]
\centering
\caption{Osmotic down TR CC}
\resizebox{1\textwidth}{!}{
\label{my-label}
\begin{tabularx}{1.4\textwidth}{|l|Y|T{-1.4}|}
\hline
term ID    & description                                & {log10 p-value} \\ \hline
GO:0000502 & proteasome complex                         & -2.6484       \\ \hline
GO:0016020 & membrane                                   & -3.8974       \\ \hline
GO:0016021 & integral component of membrane             & -1.2830       \\ \hline
GO:0012505 & endomembrane system                        & -0.2604       \\ \hline
GO:0030479 & actin cortical patch                       & -2.4783       \\ \hline
GO:0043332 & mating projection tip                      & -1.1630       \\ \hline
GO:0005933 & cellular bud                               & -0.3994       \\ \hline
GO:0005739 & mitochondrion                              & -2.1914       \\ \hline
GO:0005737 & cytoplasm                                  & -0.3145       \\ \hline
GO:0034515 & proteasome storage granule                 & -1.4628       \\ \hline
GO:0005749 & mitochondrial respiratory chain complex II & -1.5398       \\ \hline
GO:0005628 & prospore membrane                          & -0.9696       \\ \hline
GO:0000407 & pre-autophagosomal structure               & -1.4259       \\ \hline
GO:0005773 & vacuole                                    & -1.9021       \\ \hline
GO:0019898 & extrinsic component of membrane            & -0.7193       \\ \hline
GO:1990112 & RQC complex                                & -0.9441       \\ \hline
GO:1990143 & CoA-synthesizing protein complex           & -0.7790       \\ \hline
GO:0005759 & mitochondrial matrix                       & -0.5562       \\ \hline
GO:0035859 & Seh1-associated complex                    & -0.2316       \\ \hline
GO:1990429 & peroxisomal importomer complex             & -0.6132       \\ \hline
GO:0000329 & fungal-type vacuole membrane               & -1.2879       \\ \hline
GO:0034657 & GID complex                                & -0.6000       \\ \hline
\end{tabularx}}
\end{table}

I would like to order automatically rows by the value of the log10 p-value column. I would like the tab to be shown with first row corresponding to row with the lowest number in log10 p-value and the last row with the largest.

Is this possible?

Werner
  • 603,163
  • 1
    scaling tables is horrible:-) but if you must scale beware adding space: you have a word spaces at the start of your resizebox due to missing %. – David Carlisle Sep 02 '15 at 17:24
  • I needed to have all tables with the same format, and columns placed in the same position with same width for several tables. I works, its fine with me. But thanks for the tip! – biorunner88 Sep 02 '15 at 17:28
  • 1
    @biorunner88, you like to have functionality of Excel program? This is directly in (La)TeX difficult to achieve (if even possible). It is not design for such a tasks. So I thing, that the simplest way is to prepare data for your table in Excel or similar program and then export data to LaTeX. – Zarko Sep 02 '15 at 17:50
  • Thanks. I was just wondering whether this could be done some way in LaTex. – biorunner88 Sep 02 '15 at 17:51
  • how did you define the Y and T column types? – Mico Sep 02 '15 at 18:29
  • @biorunner88, Your problem can be managed by good editor too. For example WinEdt has macro SORT, which can rearrange selected lines in editor. For details see Editor documentation. – Zarko Sep 02 '15 at 20:01
  • @Zarko My answer shows that it is possible to do sorting at TeX macro level. If the TeX source is prepared already and OP needs to sort table rows automatically then the macros mentioned in my answer can be used. – wipet Sep 03 '15 at 08:58
  • @wipet, I'm glad that I were wrong! It is nice (almost wizard) solution :-). Personally I have some "healthy" skepticism for such solutions. I more prefer to have similar algorithm in editor, which sort a table in the document, which can later be compiled at any place which haven't such macro. I didn't test your nor Werner's answer yet, so don't know all theirs implications on LaTeX file. – Zarko Sep 03 '15 at 18:26

2 Answers2

5

Here is a datatool implementation that reads a CSV table, sorts it and constructs the visual representation via a tabularx:

enter image description here

\documentclass{article}

\usepackage{datatool,filecontents,tabularx,booktabs}

\begin{document}

\begin{filecontents*}{term-log.csv}
%termID    , description                               , logten
GO:0000502, proteasome complex                        , -2.6484
GO:0016020, membrane                                  , -3.8974
GO:0016021, integral component of membrane            , -1.2830
GO:0012505, endomembrane system                       , -0.2604
GO:0030479, actin cortical patch                      , -2.4783
GO:0043332, mating projection tip                     , -1.1630
GO:0005933, cellular bud                              , -0.3994
GO:0005739, mitochondrion                             , -2.1914
GO:0005737, cytoplasm                                 , -0.3145
GO:0034515, proteasome storage granule                , -1.4628
GO:0005749, mitochondrial respiratory chain complex II, -1.5398
GO:0005628, prospore membrane                         , -0.9696
GO:0000407, pre-autophagosomal structure              , -1.4259
GO:0005773, vacuole                                   , -1.9021
GO:0019898, extrinsic component of membrane           , -0.7193
GO:1990112, RQC complex                               , -0.9441
GO:1990143, CoA-synthesizing protein complex          , -0.7790
GO:0005759, mitochondrial matrix                      , -0.5562
GO:0035859, Seh1-associated complex                   , -0.2316
GO:1990429, peroxisomal importomer complex            , -0.6132
GO:0000329, fungal-type vacuole membrane              , -1.2879
GO:0034657, GID complex                               , -0.6000
\end{filecontents*}

\DTLloaddb[noheader,keys={termID,description,logten}]{termlog}{term-log.csv}
\DTLsort{logten=ascending}{termlog}

\noindent
\begin{tabularx}{\linewidth}{ l X r }
  \toprule
  \bfseries term ID & 
    \bfseries description & 
    \bfseries $\log_{10}$ $p$-value \\
  \midrule
  \DTLforeach{termlog}{%
    \termID=termID,\termdesc=description,\logten=logten}{%
    \termID & \termdesc & $\logten$ \\
  }
  \\[-\normalbaselineskip] \bottomrule
\end{tabularx}

\end{document}
Werner
  • 603,163
  • If I understand the question right, then it is about a possibility of automatically sorting table rows when tables are in LaTeX source prepared already. No about "how to change input", "what external program can be used" and "how to change output". But maybe I don't understand well, because I see that your answer has more votes... – wipet Sep 03 '15 at 09:04
4

I copied the \mergesort macro from OPmac macro package for sorting. You can try this:

\documentclass{article}

\long\def\addto#1#2{\expandafter\def\expandafter#1\expandafter{#1#2}}
\def\sdef#1{\expandafter\def\csname#1\endcsname}
\def\removeiilist{\gdef\iilist{}}

\def\mergesort #1#2,#3{% by Miroslav Olsak
   \ifx,#1%                      % prazdna-skupina,neco,  (#2=neco #3=pokrac)
      \addto\iilist{#2,}%        % dvojice skupin vyresena
      \return{\fif\mergesort#3}%    % \mergesort pokracovani
   \fi
   \ifx,#3%                      % neco,prazna-skupina,  (#1#2=neco #3=,)
      \addto\iilist{#1#2,}%      % dvojice skupin vyresena
      \return{\fif\mergesort}%      % \mergesort dalsi
   \fi
   \ifx\end#3%                   % neco,konec (#1#2=neco)
      \ifx\empty\iilist                % neco=kompletni setrideny seznam
         \def\iilist{#1#2}%
         \return{\fif\fif\gobbletoend}%   % koncim
      \else                      % neco=posledni skupina nebo \end
         \return{\fif\fif \expandafter\removeiilist % spojim \iilist+neco znova
                \expandafter\mergesort\iilist#1#2,#3}%
   \fi\fi                      % zatriduji: p1+neco1,p2+neco2,
   \isAleB #1#3\ifAleB         % p1<p2
      \addto\iilist{#1}%       % p1 do bufferu
      \return{\fif\mergesort#2,#3}%         % \mergesort neco1,p2+neco2,
   \else                       % p1>p2
      \addto\iilist{#3}%       % p2 do bufferu
      \return{\fif\mergesort#1#2,}%         % \mergesort p1+neco1,neco2,
   \fi
   \relax % zarazka, na ktere se zastavi \return
}
\def\return#1#2\fi\relax{#1} \def\fif{\fi}
\def\gobbletoend #1\end{}
\def\isAleB#1#2#3{\ifnum#1<#2}

\newcount\trnum

\def\trstart{\par \trnum=0 \def\iilist{}}
\def\tr #1&#2&#3.#4\\{\advance\trnum by1
   \sdef{tr:\the\trnum}{#1&#2&#3.#4\\\hline}%
   \sdef{trs:\the\trnum}{#3#4}%
   \expandafter\addto\expandafter\iilist\expandafter{%
      \csname trs:\the\trnum\endcsname,}%
}
\def\rewriteiilist#1{\ifx#1\relax \else 
   \expandafter\rewriteiilistA\string#1\relax \expandafter\rewriteiilist\fi}
\def\rewriteiilistA#1:#2\relax{%
   \expandafter\addto\expandafter\iilist\expandafter{\csname tr:#2\endcsname}}
\def\trsort{%
   \expandafter\removeiilist \expandafter\mergesort \iilist \end,\end
   \expandafter\removeiilist \expandafter\rewriteiilist \iilist \relax
}

\begin{document}

\trstart
\tr GO:0000502 & proteasome complex                         & -2.6484       \\
\tr GO:0016020 & membrane                                   & -3.8974       \\
\tr GO:0016021 & integral component of membrane             & -1.2830       \\
\tr GO:0012505 & endomembrane system                        & -0.2604       \\
\tr GO:0030479 & actin cortical patch                       & -2.4783       \\
\tr GO:0043332 & mating projection tip                      & -1.1630       \\
\tr GO:0005933 & cellular bud                               & -0.3994       \\
\tr GO:0005739 & mitochondrion                              & -2.1914       \\
\tr GO:0005737 & cytoplasm                                  & -0.3145       \\
\tr GO:0034515 & proteasome storage granule                 & -1.4628       \\
\tr GO:0005749 & mitochondrial respiratory chain complex II & -1.5398       \\
\tr GO:0005628 & prospore membrane                          & -0.9696       \\
\tr GO:0000407 & pre-autophagosomal structure               & -1.4259       \\
\tr GO:0005773 & vacuole                                    & -1.9021       \\
\tr GO:0019898 & extrinsic component of membrane            & -0.7193       \\
\tr GO:1990112 & RQC complex                                & -0.9441       \\
\tr GO:1990143 & CoA-synthesizing protein complex           & -0.7790       \\
\tr GO:0005759 & mitochondrial matrix                       & -0.5562       \\
\tr GO:0035859 & Seh1-associated complex                    & -0.2316       \\
\tr GO:1990429 & peroxisomal importomer complex             & -0.6132       \\
\tr GO:0000329 & fungal-type vacuole membrane               & -1.2879       \\
\tr GO:0034657 & GID complex                                & -0.6000       \\
\trsort
\begin{tabular}{|l|l|l|}
\hline
term ID    & description                           & {log10 p-value} \\ \hline
\iilist
\end{tabular}

\end{document}

EDIT If you don't want to do any great changes in your LaTeX source file then you can add the \SORTmyTAB macro definition to other definitions above:

\def\SORTmyTAB#1\hline#2\hline{%
   \def\trhead{#2}%
   \trstart
   \trx
}
\def\trx#1{\ifx\end#1\expandafter\trend \else
   \expandafter\trxA\expandafter#1\fi
}
\def\trxA#1\hline{\tr#1\unskip\trx}
\def\trend#1{%
   \trsort
   \begin{tabular}{|l|l|r|}  % Normal LaTeX construction, maybe differ
   \hline
   \trhead \hline
   \iilist
   \end{tabular}
}

and you can put only the \SORTmyTAB control sequence right before your tabular environment. For example:

\begin{table}[]
\centering
\caption{Osmotic down TR CC}
\resizebox{1\textwidth}{!}{%
\label{my-label}%
\SORTmyTAB
\begin{tabularx}{1.4\textwidth}{|l|Y|T{-1.4}|}
\hline
term ID    & description                                & {log10 p-value} \\ \hline
GO:0000502 & proteasome complex                         & -2.6484       \\ \hline
GO:0016020 & membrane                                   & -3.8974       \\ \hline
GO:0016021 & integral component of membrane             & -1.2830       \\ \hline
GO:0012505 & endomembrane system                        & -0.2604       \\ \hline
GO:0030479 & actin cortical patch                       & -2.4783       \\ \hline
GO:0043332 & mating projection tip                      & -1.1630       \\ \hline
GO:0005933 & cellular bud                               & -0.3994       \\ \hline
GO:0005739 & mitochondrion                              & -2.1914       \\ \hline
GO:0005737 & cytoplasm                                  & -0.3145       \\ \hline
GO:0034515 & proteasome storage granule                 & -1.4628       \\ \hline
GO:0005749 & mitochondrial respiratory chain complex II & -1.5398       \\ \hline
GO:0005628 & prospore membrane                          & -0.9696       \\ \hline
GO:0000407 & pre-autophagosomal structure               & -1.4259       \\ \hline
GO:0005773 & vacuole                                    & -1.9021       \\ \hline
GO:0019898 & extrinsic component of membrane            & -0.7193       \\ \hline
GO:1990112 & RQC complex                                & -0.9441       \\ \hline
GO:1990143 & CoA-synthesizing protein complex           & -0.7790       \\ \hline
GO:0005759 & mitochondrial matrix                       & -0.5562       \\ \hline
GO:0035859 & Seh1-associated complex                    & -0.2316       \\ \hline
GO:1990429 & peroxisomal importomer complex             & -0.6132       \\ \hline
GO:0000329 & fungal-type vacuole membrane               & -1.2879       \\ \hline
GO:0034657 & GID complex                                & -0.6000       \\ \hline
\end{tabularx}}
\end{table}

I don't comment the unusable usage of \resizebox, the visual quality of the table etc. I only answered your question and simply copied your code here.

wipet
  • 74,238