TransWikia.com

How to position number above specific column on a table

TeX - LaTeX Asked by Lindsey Moyer on April 17, 2021

I am trying to display a number outside of a table next to a certain cell. In this example I want the 2 to be above the 3rd column. Right now I am making two separate tables with the first one not having a border, but the 2 is too high above the table. I attached a picture of what I currently have and this is the code I am using as of now. What is the easiest way to achieve this?

enter image description here

begin{table}[htb!]
centering
begin{tabular}{c c c}
     &  & 2 
end{tabular}
end{table}

begin{table}[htb!]
centering
begin{tabular}{|c|c|c|}
hline
 & 1 &  
hline
 &  &  
hline
 &  &  
hline
end{tabular}
caption{3$times$3 Magic Square}
label{tab:my_label}
end{table}

4 Answers

What about making a bigger table partialy with visible borders:

documentclass{article}

begin{document}
    renewcommand{arraystretch}{1.5}
    begin{table}[]
        centering
        begin{tabular}{lllll}
            & 1                      & 2                      & 3                      &    cline{2-4}
            multicolumn{1}{l|}{12} & multicolumn{1}{l|}{A} & multicolumn{1}{l|}{B} & multicolumn{1}{l|}{C} & 4  cline{2-4}
            multicolumn{1}{l|}{11} & multicolumn{1}{l|}{D} & multicolumn{1}{l|}{E} & multicolumn{1}{l|}{F} & 5  cline{2-4}
            multicolumn{1}{l|}{10} & multicolumn{1}{l|}{G} & multicolumn{1}{l|}{H} & multicolumn{1}{l|}{I} & 6  cline{2-4}
            & 9                      & 8                      & 7                      &  
        end{tabular}
        caption{3x3 magic square}
        label{tab:my_label}
    end{table}
    
end{document}

enter image description here

Correct answer by Roland on April 17, 2021

Welcome to TEX.SE! You should be more specific about distance between number "outside" of table and table.

One possible solution is:

documentclass{article}
NewExpandableDocumentCommandmcc{O{1}m}
    {multicolumn{#1}{c}{#2}}
usepackage{array}

begin{document}
    begin{table}[htb!]
centering
renewcommandarraystretch{1.6}
begin{tabular}{|*{3}{w{c}{1em}|} }
mcc[2]{}   & mcc{2}   [1ex]
    hline
hphantom{3}
            &   1   &   
hline
            &       &   
hline
            &       &  
hline
end{tabular}
    caption{3$times$3 Magic Square}
label{tab:my_label}
    end{table}
end{document}

enter image description here

Another possible solution is use of the blkarray package:

documentclass{article}
NewExpandableDocumentCommandmcc{O{1}m}
    {multicolumn{#1}{c}{#2}}
usepackage{blkarray, bigstrut}

begin{document}
begin{table}
  [
begin{blockarray}{ccc}
    &   & 2 bigstrut[b]    
begin{block}{|c|c|c|}
hline
hphantom{3}
    & 1 &       
hline
    &   &       
hline
    &   &       
hline
    end{block}
end{blockarray}
]
    caption{3$times$3 Magic Square as array}
label{tab:my_label}
    end{table}
end{document}

enter image description here

Answered by Zarko on April 17, 2021

enter image description here

documentclass{article}

usepackage{nicematrix}
usepackage{tikz}
usetikzlibrary{calc}
begin{document}
    
    begin{NiceTabular}{|c|c|}
        hline
        A & B 
        hline
        C & D 
        hline
    end{NiceTabular}
    
    
    bigskip
    begin{NiceTabular}{|c|c|c|}[code-after=%
        tikz draw($(row-1-|col-2)!0.5!(row-1-|col-3)$) node[above,]{2} ;]
        hline
        A & B & C hline
        E & F & G hline
        H & I & J
        hline
    end{NiceTabular}
    
end{document}

Answered by js bibra on April 17, 2021

With {NiceTabular} of nicematrix, it's easy.

documentclass{article}
usepackage{nicematrix}
begin{document}

begin{NiceTabular}{ccc}[hvlines,first-row]
  & 2 & 
A & B & C 
D & E & F 
G & H & I
end{NiceTabular}

end{document}

Output of the above code

Answered by F. Pantigny on April 17, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP