[Tex/LaTex] Referencing a table in latex

captionscross-referencingtables

My latex document is given below.

\documentclass[12pt]{report}
\linespread{}
\usepackage{fancyhdr}
\usepackage{amsmath,amssymb}
\usepackage{caption}
\usepackage{relsize}
\usepackage[font={small,it}]{caption}
\usepackage{listings}
\usepackage{color} 
\definecolor{mygreen}{RGB}{28,172,0} 
\definecolor{mylilas}{RGB}{170,55,241}
\usepackage[titletoc,toc]{appendix}
\usepackage{graphicx}
\usepackage{setspace}  
\usepackage{changepage}
\usepackage[textwidth=16cm,textheight=24cm,margin=2cm]{geometry}
\usepackage[colorlinks,citecolor=red]{hyper ref}
\hypersetup{colorlinks=true, linkcolor=blue}
\usepackage{titlesec}
\titleformat{\chapter}[display]{\Large\centering}{Chapter \thechapter:}{0pt}{}{}
\titleformat{\section}{\large\bfseries}{\thesection}{1em}{}
\titleformat{\subsection}{\large\bfseries}{\thesubsection}{1em}{}
\titleformat{\subsubsection}{\large\bfseries}{\thesubsubsection}{1em}{}
\pagestyle{fancy}
\fancyhead[RE,RO]{}
\fancyhead[LO,RE]{\itshape \nouppercase \leftmark}
\usepackage{float}
\restylefloat{table}
\usepackage[table]{xcolor} 
\setlength{\arrayrulewidth}{1mm}  
\renewcommand{\arraystretch}{1.2} 
\setlength{\tabcolsep}{0.5cm} 
\usepackage{booktabs}
\usepackage{tabularx}
\usepackage{array}
\usepackage{lmodern}
\usepackage{microtype} 
\usepackage{siunitx}
\begin{document}
\begin{table}[h]
\caption{Parameters}
\begin{tabular}{ p{6cm} p{4cm} p{4cm}  }    
\hline \\ 
\textsc{Demo 1} \\
Characteristics & \textsc{Water} & \textsc{Air}    \\ 
\hline 
Density & 1000 & 1.25 \\
State  &  liquid & Gas \\
\hline
\end{tabular}
\caption*{Table 1}
\label{table1}
\end{table}
\end{document}

In the above table I would like to make reference to the table using the "table1" label in the code. However, when I use \ref{table1}, it does not seem to refer me to the table. Also, The first caption on top (\caption{Parameters}) does not seem to appear in my table. I would really appreciate any help with these issues. I must be missing something quite obvious.

Thanks

Best Answer

\restylefloat causes the caption positioning problem and \label after \caption*{} is useless!

\documentclass[12pt]{report}
\linespread{}
\usepackage{fancyhdr}
\usepackage{amsmath,amssymb}
\usepackage{relsize}
\usepackage[font={small,it}]{caption}
\usepackage{listings}
\usepackage{color} 
\definecolor{mygreen}{RGB}{28,172,0} 
\definecolor{mylilas}{RGB}{170,55,241}
\usepackage[titletoc,toc]{appendix}
\usepackage{graphicx}
\usepackage{setspace}  
\usepackage{changepage}
\usepackage[textwidth=16cm,textheight=24cm,margin=2cm]{geometry}
\usepackage[colorlinks,citecolor=red]{hyper ref}
\hypersetup{colorlinks=true, linkcolor=blue}
\usepackage{titlesec}
\titleformat{\chapter}[display]{\Large\centering}{Chapter \thechapter:}{0pt}{}{}
\titleformat{\section}{\large\bfseries}{\thesection}{1em}{}
\titleformat{\subsection}{\large\bfseries}{\thesubsection}{1em}{}
\titleformat{\subsubsection}{\large\bfseries}{\thesubsubsection}{1em}{}
\pagestyle{fancy}
\fancyhead[RE,RO]{}
\fancyhead[LO,RE]{\itshape \nouppercase \leftmark}
\usepackage{float}
%\restylefloat{table}
\usepackage[table]{xcolor} 
\setlength{\arrayrulewidth}{1mm}  
\renewcommand{\arraystretch}{1.2} 
\setlength{\tabcolsep}{0.5cm} 
\usepackage{booktabs}
\usepackage{tabularx}
\usepackage{array}
\usepackage{lmodern}
\usepackage{microtype} 
\usepackage{siunitx}

\begin{document}

\begin{table}[h]
\caption{Parameters}
\begin{tabular}{ p{6cm} p{4cm} p{4cm}  }    
\hline \\ 
\textsc{Demo 1} \\
Characteristics & \textsc{Water} & \textsc{Air}    \\ 
\hline 
Density & 1000 & 1.25 \\
State  &  liquid & Gas \\
\hline
\end{tabular}
%\caption*{Table 1}
\label{table1}
\end{table}


\begin{table}[h]
\begin{tabular}{ p{6cm} p{4cm} p{4cm}  }    
\hline \\ 
\textsc{Demo 1} \\
Characteristics & \textsc{Water} & \textsc{Air}    \\ 
\hline 
Density & 1000 & 1.25 \\
State  &  liquid & Gas \\
\hline
\end{tabular}
\caption*{Parameters}
\label{table2} %%% Useless -- done on purpose here!!!
\end{table}
Table \ref{table1} and another Table \ref{table2} has no influence
\end{document}

enter image description here

Related Question