Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

I know there exists a standard code for a Horner scheme. But I would like to get the following tabular.

enter image description here

I've got the next code but I can't find how I get that little vertical line at the right bottom.

\begin{tabular}{c|cccc}  
        & 3 & 2 & -5 & -10 \\   
    &   &   &    &     \\ \hline  
        &   &   &    &    
\end{tabular}  

I've got another question about this. The number 11 is now shifted to the left, but I want that it's under 16.

\begin{tabular}{c|cccc}  
  & 3 & 2 & -5 & -10 \\   
  2 & $\downarrow$  & 6  & 16    & 22   \\ \hline  
  & 3  & 8  &  11   \multicolumn{1}{c|}{}  & 12   
\end{tabular}
share|improve this question
    
You should use \[ \begin{array}{c|rrrr} & 3 & 2 & -5 & -10 \\ 2 & \downarrow & 6 & 16 & 22 \\ \hline & 3 & 8 & \multicolumn{1}{c|}{11} & 12 \end{array} \] otherwise, minus signs will be considered as mere hyphens and the results won't be good. –  Gonzalo Medina Mar 14 at 18:11
    
Thank you! It works! –  Silke Mar 15 at 10:40

2 Answers 2

up vote 10 down vote accepted
\documentclass{article}
\begin{document}
  $\begin{array}{c|cccc}
        & 3 & 2 & -5 & -10 \\
    &   &   &    &     \\ \hline
        &   &   & \multicolumn{1}{c|}{}   &
\end{array}$
\end{document}

enter image description here

If you want it to be thick,

\documentclass{article}
\usepackage{array}
\begin{document}
  $\begin{array}{c|cccc}
        & 3 & 2 & -5 & -10 \\
    &   &   &    &     \\ \hline
        &   &   &    & \multicolumn{1}{!{\vrule width 1.5pt}c}{}
\end{array}$
\end{document}

enter image description here

share|improve this answer
1  
You should use array or math mode for the entries anyway. –  egreg Mar 14 at 16:32
1  
The negative sign looks bad in text mode. :-) –  In PSTricks we trust Mar 14 at 17:41

Just for fun, a macro that also computes the coefficients (only integers allowed).

\documentclass{article}
\usepackage{xparse}

\ExplSyntaxOn
\NewDocumentCommand{\horner}{smm}
 {
  \IfBooleanTF{#1}
   { \bool_set_false:N \l_silke_show_bool }
   { \bool_set_true:N \l_silke_show_bool }
  \silke_horner:nn { #2 } { #3 }
 }

\bool_new:N \l_silke_show_bool
\seq_new:N \l_silke_top_seq
\seq_new:N \l_silke_middle_seq
\seq_new:N \l_silke_bottom_seq
\seq_new:N \l_silke_temp_seq
\int_new:N \l_silke_degree_int
\tl_new:N \l_silke_remainder_tl

\cs_new_protected:Npn \silke_horner:nn #1 #2
 {
  \seq_set_split:Nnn \l_silke_top_seq { , } { #1 }
  \int_set:Nn \l_silke_degree_int { \seq_count:N \l_silke_top_seq }
  \seq_clear:N \l_silke_middle_seq
  \seq_clear:N \l_silke_bottom_seq
  \seq_put_right:Nn \l_silke_middle_seq { \downarrow }
  \seq_put_right:Nx \l_silke_bottom_seq
   {
    \int_to_arabic:n { \seq_item:Nn \l_silke_top_seq { 1 } }
   }
  \int_step_inline:nnnn { 2 } { 1 } { \l_silke_degree_int }
   {
    \seq_put_right:Nx \l_silke_middle_seq
     {
      \int_to_arabic:n { \seq_item:Nn \l_silke_bottom_seq { ##1 - 1 } * #2 }
     }
    \seq_put_right:Nx \l_silke_bottom_seq
     {
      \int_to_arabic:n
       {
        \seq_item:Nn \l_silke_top_seq { ##1 }
        +
        \seq_item:Nn \l_silke_middle_seq { ##1 }
       }
     }
   }
  \silke_print_scheme:n { #2 }
 }

\cs_new_protected:Npn \silke_print_scheme:n #1
 {
  \bool_if:NF \l_silke_show_bool
   {
    \silke_phantom:N \l_silke_middle_seq
    \silke_phantom:N \l_silke_bottom_seq
   }
  \seq_pop_right:NN \l_silke_bottom_seq \l_silke_remainder_tl
  \begin{array}{r | *{\l_silke_degree_int}{r} }
     & \seq_use:Nn \l_silke_top_seq { & } \\
  #1 & \seq_use:Nn \l_silke_middle_seq { & } \\
  \hline
     & \seq_use:Nn \l_silke_bottom_seq { & } & 
       \multicolumn{1}{@{\vline width 1pt}r}{\l_silke_remainder_tl}
  \end{array}
 }

\cs_new_protected:Npn \silke_phantom:N #1
 {
  \seq_clear:N \silke_temp_seq
  \seq_map_inline:Nn #1 { \seq_put_right:Nn \silke_temp_seq { \phantom{##1} } }
  \seq_set_eq:NN #1 \silke_temp_seq
 }

\ExplSyntaxOff

\begin{document}

$\horner{3,2,-5,-10}{2}$

$\horner*{3,2,-5,-10}{2}$

$\horner{1,5,10,10,5,1}{-1}$

\end{document}

The *-variant just reserves the space, but doesn't print the numbers in the middle and bottom rows.

enter image description here

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.