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}
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.

| improve this answer | |
  • 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 6:15
  • 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 8:21
  • 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
  • 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
  • 1
    @Mico ok. solution is \usepackage{float} \begin{table}[H]... – Antonio Nov 14 '18 at 16:40

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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