0

I have created a command to generate a table with 7 arguments since I need to repeat it many times with different content. The problem I have is that when I create the table using this command, it is always placed at the top of the page. I have loaded the floats package, I have added H in the table header, I have also tried with !H but it returns an error ... I cannot find the cause of this Latex behavior regarding the position of a table with respect to the page. I add the definition code of the command:

\newcommand{\PTgen}[7]{
\begin{table}[htbp]
    \extrarowheight = -0.5ex
    \renewcommand{\tabcolsep}{6pt}
    \renewcommand{\arraystretch}{3}
    \renewcommand {\tabcolsep}{3pt}
    \renewcommand{\arrayrulewidth}{3pt}
    \centering
    \begin{tabular}{ c |  c | c }
        $ #1 $ &$  #2 $ & \\ \hline
        $ #3 $ & $ #4 $ & \cellcolor[gray]{0.9} $ #5 $ \\ \hline
        & \cellcolor[gray]{0.9} $ #6 $ & \cellcolor[gray]{0.9} $ #7 $ \\
    \end{tabular}
\end{table} 
}

Invoking the command:

\PTgen{1} {2} {3} {4} {5} {6} {7}

I would appreciate any suggestions that help me solve the problem.

  • 1
    What happens when you use htbp as float specifier (which should be the default)? – Werner Mar 14 '21 at 22:47
  • I've tried it, Werner, but the problem persists ... – Jesús Álvarez Lobo Mar 14 '21 at 22:52
  • unrelated to the float position but you are missing % at ends of lines here and \extrarowheight = -0.5ex %Ajusta espacio superior entre filas para centrados will, in general, cause rows in the table to over-print the row above with no warning. – David Carlisle Mar 14 '21 at 22:53
  • 1
    The only purpose of the table environment is to specify the tabular can be moved, if you do not want it to move, why are you using table? – David Carlisle Mar 14 '21 at 23:00
  • @JesúsÁlvarezLobo: Then we'd need more detail, like a complete, minimal example, that replicates your current issue. – Werner Mar 14 '21 at 23:02
  • Please don't edit your question in a way that fundamentally alters the question after answers have been posted, it makes it impossible for people to understand the posted answer. – David Carlisle Mar 14 '21 at 23:09
  • Thank you, David Carlisle, but if I delete the table all the text is centered! (?) – Jesús Álvarez Lobo Mar 14 '21 at 23:27
  • David, all I did was remove the comments preceded by % to prove that they don't need to end in % as you suggested. Thank you for your intention to help. – Jesús Álvarez Lobo Mar 14 '21 at 23:46
  • Solved! ... with the sugestion of David Carlisle: I removed the table and substituted \centering by \begin{center}... \end{center} – Jesús Álvarez Lobo Mar 15 '21 at 00:38

1 Answers1

5

[H] is a special variant that should only be used sparingly and must be used alone, you can not use [!H]

Unrelated here but even with normal float arguments such as htbp You should not routinely add ! as ! means ignore the document-specified constraints for this float. It does not make sense to ignore the constraints on every float: better to not use ! and set more suitable constraints.

 \renewcommand {\tabcolsep}{3pt} %Ajusta espacio interior entre columnas
 \renewcommand{\arrayrulewidth}{3pt} %Ajusta el grueso de línea

Both of these are lengths and must be set with \setlength If you redefine them to be macros as here you will break latex's table code.

for example the standard (non-array package) tabular has

    \@addtopreamble{\hskip .5\arrayrulewidth}%

when \arrayrulewidth is a length this adds half its width. With your definition it expands to

    \@addtopreamble{\hskip .53pt}%

which accidentally is a valid length but not the intended length of half of 3pt/

David Carlisle
  • 757,742