Seite 1 von 1

Probleme mit sortierter Tabelle

Verfasst: Di 14. Jun 2016, 13:13
von alibi
Hallo Leute, ich habe ein Problem mit einem Skript für eine Tabelle welches ich aus dem Internet kopiert habe. Die Tabelle sortiert sich automatisch alphabetisch. Hier mal das Minimalbeispiel:
\documentclass{article}
\usepackage{etoolbox}
\usepackage{booktabs}
\makeatletter
% #1 - comparator
% #2 - token list to sort
\newcommand\sort[2]{%
        \ifstrempty{#2}
        {}% else
        {%
                \sort@begin#1{}#2\sort@s\sort@begin
        }%
}

% helpers
\def\sort@s{\sort@s}
\def\ifsort@s#1{%
        \ifx\sort@s#1%
                \expandafter\@firstoftwo
        \else
                \expandafter\@secondoftwo
        \fi
}

% #1 - comparator
% #2 - tokens processed so far
% #3 - smallest token so far
% #4 - rest of the list
\def\sort@begin#1#2#3#4\sort@begin{%
        \ifsort@s{#4}
        {%
                \sortend{#3}%
                \sort#1{#2}%
        }% else
        {%
                \sort@go#1{#2}{#3}#4\sort@go
        }%
}

%     #1 - comparator
% #2 - tokens processed so far
% #3 - smallest token so far
% #4 - token under consideration
% #5 - rest of the list
\def\sort@go#1#2#3#4#5\sort@go{%
        #1{#3}{#4}{\sort@output#1{#2}{#5}}%
}
% #1 - comparator
% #2 - tokens processed so far
% #3 - rest of the list
% #4 - smaller of the two tokens
% #5 - larger of the two tokens
\def\sort@output#1#2#3#4#5{%
        \ifsort@s{#3}
        {%
                \sortoutput{#4}%
                \sort#1{#2{#5}}%
        }% else
        {%
                \sort@begin#1{#2{#5}}{#4}#3\sort@begin
        }%
}

\def\sort@numlt#1#2#3{%
        \ifnumcomp{#1}<{#2}
        {#3{#1}{#2}}% else
        {#3{#2}{#1}}%
}

\def\sort@numgt#1#2#3{%
        \ifnumcomp{#1}>{#2}
        {#3{#1}{#2}}% else
        {#3{#2}{#1}}%
}

\def\sort@alpha#1#2#3{%
        \ifnumcomp{\pdfstrcmp{#1}{#2}}<{0}
        {#3{#1}{#2}}% else
        {#3{#2}{#1}}%
}

\def\sort@firstcol#1#2{%
    \expandafter\expandafter\expandafter
        \sort@fc@helper\expandafter#1#2#1#2%
}

\def\sort@fc@helper#1&#2\\#3&#4\\#5#6#7{%
    \ifnumcomp{\pdfstrcmp{#1}{#3}}<{0}
    {#7{#5}{#6}}% else
    {#7{#6}{#5}}%
}

\newcommand*\sortnumlt{\sort\sort@numlt}
\newcommand*\sortnumgt{\sort\sort@numgt}
\newcommand*\sortalpha{\sort\sort@alpha}
\newcommand*\sortfirstcol{\sort\sort@firstcol}
\makeatother

% Change these to change out the sort outputs.
\newcommand*\sortoutput[1]{#1}
\newcommand*\sortend[1]{#1}

\begin{document}

\def\rowa  {$m$ & $kg$ & Masse \\}
\def\rowb  {$F$ & $N$ & Kraft \\}
\def\rowc  {$h$ & $m$ & Höhe \\}
\begin{tabular}{ccc}
\toprule
\textbf{Variable} & \textbf{SI-Einheit} & \textbf{Bedeutung}\\
\midrule
\sortfirstcol{\rowa\rowb\rowc}
\bottomrule
\end{tabular}

\end{document}
Nun habe ich die folgenden 2 Probleme: Die Tabelle hat zu viele Einträge und passt nicht mehr auf eine Seite, sie erweitert sich aber nicht automatisch auf 2 Seiten, sondern schneidet den Rest einfach ab. Das andere Problem ist, dass ich keine automatischen Zeilenumbrüche habe, falls z.B. die Bedeutung der Variable einen zu langen Namen hat. Für Hilfe wäre ich sehr dankbar :)

Verfasst: Di 14. Jun 2016, 13:28
von Gast
ltablex

Verfasst: Mi 15. Jun 2016, 11:53
von alibi
Ok, ich habe jetzt \usepackage{tabularx} und \usepackage{ltablex} hinzugefügt, außerdem tabular durch tabularx ersetzt, aber leider funktioniert dann das Dokument nicht mehr :/

Verfasst: Mi 15. Jun 2016, 12:15
von DADABBI
Du musst bei der tabularx-Umgebung natürlich auch eine (maximale) Tabellenbreite angeben und sinnvoll ist die Umgebung auch nur, wenn du min. eine X-Spalte verwendest. Sinnvoll ist dann noch, den Kopf mit \endhead zu markieren.

Angewendet auf dein Beispiel:
\documentclass{article}
\usepackage{etoolbox}
\usepackage{ltablex}
\usepackage{booktabs}
\makeatletter
% #1 - comparator
% #2 - token list to sort
\newcommand\sort[2]{%
        \ifstrempty{#2}
        {}% else
        {%
                \sort@begin#1{}#2\sort@s\sort@begin
        }%
}

% helpers
\def\sort@s{\sort@s}
\def\ifsort@s#1{%
        \ifx\sort@s#1%
                \expandafter\@firstoftwo
        \else
                \expandafter\@secondoftwo
        \fi
}

% #1 - comparator
% #2 - tokens processed so far
% #3 - smallest token so far
% #4 - rest of the list
\def\sort@begin#1#2#3#4\sort@begin{%
        \ifsort@s{#4}
        {%
                \sortend{#3}%
                \sort#1{#2}%
        }% else
        {%
                \sort@go#1{#2}{#3}#4\sort@go
        }%
}

%     #1 - comparator
% #2 - tokens processed so far
% #3 - smallest token so far
% #4 - token under consideration
% #5 - rest of the list
\def\sort@go#1#2#3#4#5\sort@go{%
        #1{#3}{#4}{\sort@output#1{#2}{#5}}%
}
% #1 - comparator
% #2 - tokens processed so far
% #3 - rest of the list
% #4 - smaller of the two tokens
% #5 - larger of the two tokens
\def\sort@output#1#2#3#4#5{%
        \ifsort@s{#3}
        {%
                \sortoutput{#4}%
                \sort#1{#2{#5}}%
        }% else
        {%
                \sort@begin#1{#2{#5}}{#4}#3\sort@begin
        }%
}

\def\sort@numlt#1#2#3{%
        \ifnumcomp{#1}<{#2}
        {#3{#1}{#2}}% else
        {#3{#2}{#1}}%
}

\def\sort@numgt#1#2#3{%
        \ifnumcomp{#1}>{#2}
        {#3{#1}{#2}}% else
        {#3{#2}{#1}}%
}

\def\sort@alpha#1#2#3{%
        \ifnumcomp{\pdfstrcmp{#1}{#2}}<{0}
        {#3{#1}{#2}}% else
        {#3{#2}{#1}}%
}

\def\sort@firstcol#1#2{%
    \expandafter\expandafter\expandafter
        \sort@fc@helper\expandafter#1#2#1#2%
}

\def\sort@fc@helper#1&#2\\#3&#4\\#5#6#7{%
    \ifnumcomp{\pdfstrcmp{#1}{#3}}<{0}
    {#7{#5}{#6}}% else
    {#7{#6}{#5}}%
}

\newcommand*\sortnumlt{\sort\sort@numlt}
\newcommand*\sortnumgt{\sort\sort@numgt}
\newcommand*\sortalpha{\sort\sort@alpha}
\newcommand*\sortfirstcol{\sort\sort@firstcol}
\makeatother

% Change these to change out the sort outputs.
\newcommand*\sortoutput[1]{#1}
\newcommand*\sortend[1]{#1}

\begin{document}

\def\rowa  {$m$ & $kg$ & Masse \\}
\def\rowb  {$F$ & $N$ & Kraft \\}
\def\rowc  {$h$ & $m$ & Höhe \\}
\begin{tabularx}{\textwidth}{ccX}
\toprule
\textbf{Variable} & \textbf{SI-Einheit} & \textbf{Bedeutung}\\
\midrule
\endhead
\sortfirstcol{\rowa\rowb\rowc}
\bottomrule
\end{tabularx}

\end{document}
Funktioniert im Online-Editor.

BTW: Zusätzlich zur [d]ltablex[/d]-Anleitung sollte man natürlich auch die [d]tabularx[/d]-Anleitung und die [d]longtable[/d]-Anleitung lesen. Schließlich ist ltablex eine Vereinigung dieser beiden Pakete.

Und zukünftig zeigst du bitte, was angeblich nicht funktioniert und zitierst ggf. auch Fehlermeldungen. "Funktioniert nicht" ist jedenfalls keine auch nur annähernd hinreichende Problembeschreibung! Siehe auch: Minimalbeispiel

Verfasst: Mi 15. Jun 2016, 14:36
von alibi
Oh man vielen Dank! Jetzt klappt alles. Tut mir leid dass meine zweite Antwort so schlecht vorbereitet war.