如何修改代码以向“图表列表”添加不同的标题?例如,
\begin{figure}[h!]
\centering
\includegraphics[height=7cm]{indir.jpg}
\caption[Caption for TOC]{Caption for report}
\label{fig:figure}
\end{figure}
和
\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}
在上面的例子中,“图表列表”标题和报告标题可以不同。
所以,在下面的代码中,没有“图表列表”部分。“图表列表”和报告中的标题相同。有什么办法可以修改吗?
\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}
谢谢。
答案1
\weirdCapFigure
图形和表格(和\weirdCapTable
)的自定义宏包含一个\caption
命令,因此您可以为宏提供一个额外的参数并将该参数传递给调用\caption
。
如果您希望目录标题(实际上是图片/表格标题列表)是可选的,那么实现会变得有点复杂。在这种情况下,您需要测试是否提供了参数,如果提供了,则应使用\caption
可选参数调用命令,否则应\caption
不使用可选参数调用命令。
默认情况下,可选参数优先。默认值由一组额外的方括号提供,例如,\newcommand{\weirdCapTable}[3][]
这意味着命令有三个参数,第一个是可选的,默认值为空。请注意,参数#1
始终被分配,要么使用提供的参数,要么使用默认参数。
借用以下代码https://tex.stackexchange.com/a/58638/您可以测试空参数并调用适当的\caption
命令。
梅威瑟:
\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}
结果: