4

How create this table using latex. table

The caption of the table should be in the left side and there is no space between the caption and the table. F2 and F3 should be in two lines. the contents of columns 1,3 and 4 are centered. I want it to be exactly like a table in the attached image.

This is my code

\documentclass{elsarticle}
\usepackage{booktabs}
\usepackage{amsmath}
\usepackage{siunitx}
\usepackage{booktabs, makecell, multirow}
\usepackage{caption}
\captionsetup[table]{format=plain,labelformat=simple,labelsep=period}%
\begin{document}
    \begin{table}[]
        \centering
        \caption{My caption}
        \label{my-label}
        \begin{tabular}{@{}llll@{}}
            \toprule
            F & Formula & f(x) & Search range \\ \midrule           
            1 & $\begin{array} {lcl} f(x) & = &  \sum_{i=1}^{n} \frac{x_i^{2}}{4000} - \prod_{i=1}^{n}cos(\frac{x_i}{\sqrt{i}}) + 1 \end{array}$       & 0    & [-100, 100]\\

            2 & $\begin{array} {lcl} f(x) & = & \frac{\pi}{n}10\sin (\pi {y_1}) 
            $+$ \sum_{i=1}^{n-1} {{( {{y_i} - 1})}^2}[{1 + 10{{\sin }^2}(\pi{y_{i + 1}})}] +(y_n -1)^{2}+\sum_{i=1}^{n}u(x_i,10,100,4)      \end{array}$    & 0    & [-100, 100]\\

            & $\begin{array} {lcl}
            u(x)
            u({x_i},a,k,m) =
            \begin{cases} 
              k{({x_i} - a)^m} & \,\,\,\,\,\,\,\,\,{x_i} > a\\
            0 & \,\,\,\,\,\,\,\,\,\, - a < {x_i} < a\\
            k{( - {x_i} - a)^m} &\,\,\,\,\,\,\,\,\, {x_i} <  - a

            \end{cases} & 

            \end{array}$ \\

            3 & $\begin{array} {lcl}   f\left( x \right) = 0.1\left\{ {{{\sin }^2}(3\pi {x_1}) + \mathop \sum \limits_{i = 1}^{n - 1} {{\left( {{x_i} - 1} \right)}^2}\left[ {1 + {{\sin }^2}(3\pi {x_i} + 1)} \right] + {{({x_n} - 1)}^2}\left[ {1 + {{\sin }^2}(2\pi {x_n})} \right]} \right\} + \sum\limits_{i = 1}^n {u({x_i},5,100,4)}     
            \end{array}$  & 0    & [-100, 100]\\
            \\ \bottomrule
        \end{tabular}
    \end{table} 
\end{document}
shdotcom
  • 53
  • 3

1 Answers1

3

Something like this?

enter image description here

\documentclass{elsarticle}
\usepackage{booktabs,amsmath,caption,array}
\newcolumntype{C}[1]{>{\centering\arraybackslash}m{#1}}
\newlength\mylen
\newcommand\myrange{$[-100, 100]$}
\settowidth\mylen{\myrange}
\usepackage{newtxtext,newtxmath}
\captionsetup[table]{format=plain,
                     labelfont=bf,
                     labelsep=newline,
                     singlelinecheck=false,
                     skip=0pt}
\begin{document}
\begin{table}
\caption{Table caption here.}
\label{my-label}
$\begin{array}{@{} clcC{\mylen} @{}}
\toprule
\text{F} & \multicolumn{1}{c}{\text{Formula}} 
         & f_{\min} & \text{Search} \text{range} \\ 
\midrule           
\text{F1} & 
\begin{aligned} 
   &f(x) = \frac{1}{4000}\sum_{i=1}^{n} x_i^2 
         -\prod_{i=1}^n \cos\Bigl(\frac{x_i}{\sqrt{i}}\Bigr) + 1
\end{aligned}       
& 0 & \myrange\\
\cmidrule{1-4}
\text{F2} & 
\begin{aligned} 
   &f(x) = \Bigl\{ \frac{\pi}{n} 10\sin (\pi y_1) 
           + \sum_{i=1}^{n-1} (y_i - 1)^2 
           \bigl[ 1 + 10\sin^2(\pi y_{i+1}) \bigr] \\
   &\qquad\quad+(y_n -1)^2 \Bigr\} +\sum_{i=1}^n u(x_i,10,100,4)\\
   &y_i=1+\frac{x_i+1}{4} \\
   &u({x_i},a,k,m) =
      \begin{cases} 
            k(x_i - a)^m & x_i > a\\
            0            & - a < x_i < a\\
            k(-x_i- a)^m & x_i <  - a
      \end{cases}
\end{aligned} 
& 0 & \myrange\\
\cmidrule{1-4}
\text{F3} & 
\begin{aligned}
   &f(x) = 0.1\Bigl\{ \sin^2(3\pi x_1) + 
       \sum_{i=1}^{n-1}(x_i-1)^2
       \bigl[1 +\sin^2(3\pi x_i + 1) \bigr] \\
   &\qquad\quad+(x_n-1)^2\bigl[1 +\sin^2(2\pi x_n) 
       \bigr] \Bigr\}
       + \sum_{i=1}^n u(x_i,5,100,4)     
\end{aligned}  
& 0 & \myrange\\
\bottomrule
\end{array}$
\end{table} 
\end{document}

Some of the main changes I made were (i) use of an array environment instead of a tabular environment, as virtually all of the contents are in math mode; (ii) use of aligned environments to house the formulas in column 2; (iii) deletion of lots and lots of pairs of curly braces that do nothing except create code clutter, deletion of \,\,\,\,\,\,\,\,\,; (iv) no auto-sizing of the curly braces and square brackets, and (v) choice of different line breaks for the long formulas.

Mico
  • 506,678
  • I have got this error: Sorry, but miktex-makemf did not succeed for the following reason: The txsys source file could not be found.... Sorry, but miktex-makepk did not succeed for the following reason: PK font txsys could not be created. The log file hopefully contains the information to get MiKTeX going again.... – shdotcom Feb 16 '18 at 06:01
  • 1
    @shdotcom - It sounds like your MikTeX installation didn't set up the packages newtxtext and newtxmath. Please try commenting out the instruction \usepackage{newtxtext,newtxmath} and recompiling. – Mico Feb 16 '18 at 06:15
  • It is working now (after commenting out the instruction \usepackage{newtxtext,newtxmath}), but the header's labels of table are not centered. If I remove first colomn, the first row "F1" is not aligned with other f(x). What I should change in the code to remove first colomn and align f(x) in the first row with others? – shdotcom Feb 16 '18 at 07:27
  • 1
    @shdotcom - Please clarify what you mean by "centered": Are you referring to the horizontal or the vertical dimension -- or both? I also don't know what you do when you "remove the first column". Do you remove just the column contents ("F", "F1", "F2", and "F3")? Or do you also change clcC{\mylen}to lcC{\mylen}, i.e., do you remove the first "c" in $\begin{array}{@{} clcC{\mylen} @{}}. And, do you change both instances of \cmidrule{1-4} to \cmidrule{1-3} (since the table now only has three columns)? Please advise. – Mico Feb 16 '18 at 08:21
  • I have removed c from "clcC", and it is working now. But, what does clcC mean? I have just start using latex a few days ago. Would you please give any reference that can help me to learn latex. Thank you very much for your help. – shdotcom Feb 16 '18 at 10:30
  • 1
    @shdotcom - clcC are the specifications of the 4 columns of the array environment used in the above code. c means "center", and "l" means "left-align". The C column type is defined in the preamble above, as a centered variant of the m column type. The m column type, in turn, is defined in the array package as a vertically centered variant of LaTeX's basic p ("paragraph") column type. Do check out the posting What are good learning resources for a LaTeX beginner? for various introductions to LaTeX. – Mico Feb 16 '18 at 11:32
  • @Mico i have problem with \usepackage{newtxtext,newtxmath}, has a conflict with some commands : Command \transp already defined. ...transp}[1]{{#1}^{\ensuremath{\mathsf{T}}}} – Antonio Nov 14 '18 at 09:23
  • @Antonio - Does the issue go away if you omit your definition of \transp? – Mico Nov 14 '18 at 10:31
  • @Mico yes issue go away. the definition is : \newcommand{\transp}[1]{{#1}^{\ensuremath{\mathsf{T}}}} – Antonio Nov 14 '18 at 10:37
  • 1
    @Antonio - If you prefer the "look" of your transpose operator over the one provided by newtxmath, you could issue the instruction \renewcommand{\transp}[1]{{#1}^{\mathsf{T}}} after loading the two font packages. Incidentall, I don't think it's necessary (or even sensible!) to utilize the \ensuremath directive here: If the \transp macro is ever executed in text mode, TeX is going to crash upon encountering the ^ superscript-initiator symbol. – Mico Nov 14 '18 at 10:52
  • @Mico Thanks for the suggestion. I should change many commands. probably my work has many writing errors that are revised. I'm not very experienced – Antonio Nov 14 '18 at 16:01
  • other problem. if I insert a new section and follow I write a table with the equations, the title of the new section is inserted after the table,look at the image. code is :\section{Riassumendo} \begin{table}\caption{Table caption here.}\label{my-label} $\begin{array}.....\end{array}$\end{table} – Antonio Nov 14 '18 at 16:31
  • 1
    @Mico ok. solution is \usepackage{float} \begin{table}[H]... – Antonio Nov 14 '18 at 16:40