0

How can I modify the codes for adding different caption to 'list of figures and tables'? For example,

\begin{figure}[h!]
  \centering
  \includegraphics[height=7cm]{indir.jpg}
  \caption[Caption for TOC]{Caption for report}
  \label{fig:figure}
\end{figure}

and

\begin{table}[!h]
\small
\centering
\caption[Caption for TOC]{Caption for report}
\begin{tabular}{cccc}
\hline
Row1    & Row2  & Row3  & Row4\\
\hline
Lorem & ipsum & dolor & sit \\
Lorem & ipsum & dolor & sit \\
\hline
\end{tabular}
\label{t1}
\end{table}

In the above examples, 'list of figures and tables' caption and report caption can be different.

So, In the codes below, there is no section for the 'list of figures and tables'. The caption is shown as same in 'list of figures and tables' and report. Is there any way to modify?

\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage{graphicx}
\usepackage{tabularx}

\usepackage{caption} 
\captionsetup{format=hang}

\title{Title}

\begin{document}

\section{Introduction}

\newcommand{\weirdCapFigure}[2]{\sbox{0}{#1}#1
  \rule{\dimexpr.5\linewidth-0.5\wd0}{0pt}%
  \parbox[t]{\dimexpr.5\linewidth+0.5\wd0}{\caption{#2}}
}
\newcommand{\weirdCapTable}[2]{\sbox{0}{#2}
  \rule{\dimexpr.5\linewidth-0.5\wd0}{0pt}%
  \parbox[t]{\dimexpr.5\linewidth+0.5\wd0}{\caption{#1}}
  #2%
}

\begin{figure}[h!]
  \centering
  \weirdCapFigure{\includegraphics[height=7cm]{example-image}}
  {Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed felis risus, ullamcorper ac suscipit ac, molestie non orci. Nam neque orci, facilisis in euismod sed, suscipit quis dolor.\label{fig:heyelenturkiye}}  
\end{figure}


\begin{table}[!h]
\small
\centering
\weirdCapTable{Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed felis risus, ullamcorper ac suscipit ac, molestie non orci. Nam neque orci, facilisis in euismod sed, suscipit quis dolor.\label{t1}}
{\begin{tabular}{cccc}
\hline
Row1    & Row2  & Row3  & Row4\\
\hline
Lorem & ipsum & dolor & sit \\
Lorem & ipsum & dolor & sit \\
Lorem & ipsum & dolor & sit \\
Lorem & ipsum & dolor & sit \\
\hline
\end{tabular}}
\end{table}

\end{document}

Thank you.

2

The custom macros for figures and tables (\weirdCapFigure and \weirdCapTable) contain a \caption command, so you can provide an extra argument to the macros and pass that argument to the \caption call.

If you want the TOC caption (actually List of Figures/Tables caption) to be optional, then the implementation becomes a bit more complicated. In that case you need to test whether the argument has been supplied, and if it is supplied then you should call \caption command with the optional argument, otherwise you should call \caption without the optional argument.

By default optional arguments come first. A default value is supplied with an extra set of square brackets, for example \newcommand{\weirdCapTable}[3][] which means that the command has three arguments, the first is optional, and the default value is empty. Note that the argument #1 is always assigned, either with the supplied argument or with the default argument.

Borrowing some code from https://tex.stackexchange.com/a/58638/ you can test for an empty argument and call the appropriate \caption command.

MWE:

\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage{graphicx}
\usepackage{tabularx}

\usepackage{caption} 
\captionsetup{format=hang}

\title{Title}

\begin{document}
\listoffigures
\listoftables


\section{Introduction}

\newcommand{\weirdCapFigure}[3][]{\sbox{0}{#2}#2
  \rule{\dimexpr.5\linewidth-0.5\wd0}{0pt}%
  \parbox[t]{\dimexpr.5\linewidth+0.5\wd0}{%
  \if\relax\detokenize{#1}\relax%
  \caption{#3}%
  \else%
  \caption[#1]{#3}%
  \fi}
}
\newcommand{\weirdCapTable}[3][]{\sbox{0}{#3}
  \rule{\dimexpr.5\linewidth-0.5\wd0}{0pt}%
  \parbox[t]{\dimexpr.5\linewidth+0.5\wd0}{%
  \if\relax\detokenize{#1}\relax%
  \caption{#2}%
  \else%
  \caption[#1]{#2}%
  \fi}
  #3%
}

\begin{figure}[h]
  \centering
  \weirdCapFigure[caption for list of figures]{\includegraphics[height=7cm]{example-image}}
  {Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed felis risus, ullamcorper ac suscipit ac, molestie non orci. Nam neque orci, facilisis in euismod sed, suscipit quis dolor.\label{fig:heyelenturkiye}}  
\end{figure}

\begin{figure}[h]
  \centering
  \weirdCapFigure{\includegraphics[height=7cm]{example-image}}
  {Figure without optional caption}  
\end{figure}


\begin{table}[h]
\small
\centering
\weirdCapTable[caption for list of tables]{Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed felis risus, ullamcorper ac suscipit ac, molestie non orci. Nam neque orci, facilisis in euismod sed, suscipit quis dolor.\label{tab:t1}}
{\begin{tabular}{cccc}
\hline
Row1    & Row2  & Row3  & Row4\\
\hline
Lorem & ipsum & dolor & sit \\
Lorem & ipsum & dolor & sit \\
Lorem & ipsum & dolor & sit \\
Lorem & ipsum & dolor & sit \\
\hline
\end{tabular}}
\end{table}

\begin{table}[h]
\small
\centering
\weirdCapTable{Table without optional caption.\label{tab:t2}}
{\begin{tabular}{cccc}
\hline
Row1    & Row2  & Row3  & Row4\\
\hline
Lorem & ipsum & dolor & sit \\
Lorem & ipsum & dolor & sit \\
Lorem & ipsum & dolor & sit \\
Lorem & ipsum & dolor & sit \\
\hline
\end{tabular}}
\end{table}

\end{document}

Result:

enter image description here

  • It is exactly what I meant. Thank you very much. Tables and figures without optional caption are not aligned to the left of the tables and the figures. But it is not problem, I use with caption. – volkan Jun 14 at 14:14
  • @volkan For that you can put \captionsetup{justification=raggedright,singlelinecheck=false} in the preamble, see for example tex.stackexchange.com/questions/275131/…. – Marijn Jun 14 at 14:40
  • Thank you again (: Is it possible to align text to both side? [Link]: i.stack.imgur.com/GuXKe.png Captions which have two or more lines are seems like weird. – volkan Jun 14 at 20:38
  • For that you can use justification=justified. – Marijn Jun 14 at 21:00

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.