3

I want to have a table for a Newton-Iteration. The first and the last value should be colored. So I tried the following code, and it works. But the colored numbers are not correctly set and table-number-alignment=left, seems to have no effect.

Thanks for help!

enter image description here

\documentclass[fleqn,dvipsnames,x11names,svgnames,12pt]{article}
\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}
\usepackage[ngerman]{babel}
\usepackage{amsmath}
\usepackage[upint,sansmath]{libertinust1math}  % Mathe Font
\usepackage[sb,sfdefault]{libertine}  % für Textschrift in rm und sf
\usepackage{soul}
\usepackage{etex} % um die Anzahl der Register zu erhöhen (sonst nur 256)
\usepackage{eurosym}
\usepackage{xcolor}
\usepackage{booktabs}  % für bessere Abstände in Tabellen
\usepackage{calc}      % Zum Rechnen
\usepackage{array}     % Für arrays
\usepackage{tabularx}  % Für erweiterte Tabellenoptionen
\usepackage{colortbl}  % Für farbige Tabellen
\usepackage{ragged2e}  % Für besseres links-, rechtsbündig
\usepackage{numprint}
\selectlanguage{ngerman}
\usepackage{spreadtab} % Berechnet Werte in einer Tabelle
\STsetdecimalsep{,}
\usepackage{siunitx}
\sisetup{group-separator={\,},output-decimal-marker = {,}}
\DeclareMathSymbol{,}{\mathord}{letters}{"3B}
\parindent0pt
\begin{document}
\renewcommand{\arraystretch}{1.2}
\sisetup{detect-display-math=true,
%round-mode=places,
%round-precision=9,
table-number-alignment=left,
table-format=1.9,
table-figures-integer = 1,
table-figures-decimal = 9,
}
\STautoround{9}
%\renewcommand\STprintnum[1]{\num{#1}}
\def\mcc#1{\multicolumn{1}{c|}{$#1$}}
\def\mccl#1{\multicolumn{1}{|c|}{$#1$}}
\begin{spreadtab}[\STsavecell{\Ergeb}{e6}]{{tabular}{|S[table-format=1.0]|S|S|S[table-format=2.9,table-figures-integer = 2]|S|}}\hline
@\mccl{n}                  & @\mcc{x_{n}}   & @\mcc{f(x_{n})=x^{4}-3x-3} & @\mcc{f^{\prime}(x_{n})=4x^{3}-3}  &@\mcc{x_{n+1}}  \\\hline
0                                    & {\color{BrickRed}$\num{:={1.7}}$} & \STcopy{v}{b2*b2*b2*b2-3*b2-3} & \STcopy{v}{4*b2*b2*b2-b2} & \STcopy{v}{b2-c2/d2} \\\hline
\STcopy{v}{[0,-1]+1} & \STcopy{v}{e2} &   &  &   \\\hline
                                       &                                            &   & &    \\\hline
                                       &                                            &   &  &   \\\hline
                                       &                                            &   &  & {\color{BrickRed}\num{:={}}}   \\\hline
\end{spreadtab}
\\[5pt]
$ x\approx \num{\Ergeb}$

\end{document}
2
  • as a workaround for your left alignment problem: S[table-space-text-post={xxxx}] Oct 21, 2017 at 8:55
  • @samcarter this is helpful for the third column, thank you.
    – user139826
    Oct 21, 2017 at 10:30

1 Answer 1

7
+100

For the coloured cells you can use a \multicolumn with left alignment and include the value via \tablenum:

\multicolumn{1}{l|}{\color{BrickRed}\tablenum[table-format=1.9]{:=1.7}}

This is probably best package up in a local macro, which is what I have done in the code below.

Sample output

\documentclass[12pt]{article}

\usepackage[dvipsnames]{xcolor}
\usepackage{spreadtab}
\usepackage{siunitx}

\sisetup{group-separator={\,},output-decimal-marker = {,}}
\DeclareMathSymbol{,}{\mathord}{letters}{"3B}

\begin{document}

\sisetup{detect-display-math=true,
table-number-alignment=left,
table-format=1.9,
table-figures-integer = 1,
table-figures-decimal = 9,
round-mode=places,
round-precision=9
}

\newcommand{\mBRl}[1]{\multicolumn{1}{l|}{\color{BrickRed}\tablenum[table-format=1.9]{#1}}}

\noindent
\begin{spreadtab}[\STsavecell{\Ergeb}{e6}]{{tabular}{
  |S[table-format=1.0]
  |S
  *2{|S[table-format=2.9, table-figures-integer=2]}
  |S|}}
  \hline
  @{$n$} & @{$x_{n}$}& @{$f(x_{n})=x^{4}-3x-3$} &
  @{$f'(x_{n})=4x^{3}-3$}
  &@{$x_{n+1}$} \\
  \hline
  0
  & \mBRl{:=1.7}
  & \STcopy{v}{b2*b2*b2*b2-3*b2-3} &
  \STcopy{v}{4*b2*b2*b2-b2} &
  \STcopy{v}{b2-c2/d2} \\
  \hline
  \STcopy{v}{[0,-1]+1} & \STcopy{v}{e2} & & & \\\hline & & & & \\
  \hline
  & & & & \\
  \hline
  & & & & \mBRl{:={}} \\
  \hline
\end{spreadtab}

\smallskip\noindent
$ x\approx \num{\Ergeb}$

\bigskip
\begin{tabular}{SS}
  {Long header is long}&{Short}\\
  1.87&1.98\\
  1.33&1.1
\end{tabular}
\end{document}

I have stripped away inessential parts of your code. Your \mcc macros are overly complicated, headers can just be included in braces in S columns and then they will automatically be centered.

The above also includes demonstration of an ordinary table with S columns that show that the alignment problem is occurs in any column with a very wide header.

Sample output 2

As samcarter says you can "fix" this with a specification such as

S[table-space-text-post={\hspace{4em}}]

Sample output 3

3
  • Many compliments. +1.
    – Sebastiano
    Oct 21, 2017 at 10:41
  • @AndrewSwann this works nice for this example, many thanks. Im tried this with a negative startvalue and this seems to work too. Thank you!
    – user139826
    Oct 21, 2017 at 11:26
  • @AndrewSwann Thank you for your understandable explanation.
    – user139826
    Oct 21, 2017 at 11:30

You must log in to answer this question.