如果标题参数没有以句号结尾,则插入句号

如果标题参数没有以句号结尾,则插入句号

我有一个命令\Notes,它只是一个图形下的标题:

\newcommand{\Notes}[1]{\captionsetup{position=below}\caption*{#1}}

我希望标题中的标点符号保持一致,始终以句号结尾。也就是说,我希望以下 MWE 出现Notes.在第一个图中,以及Notes with full stop.(原样)出现在第二个图中。LaTeX 可以强制执行此策略吗?

\documentclass{scrartcl}
\usepackage{caption}
\usepackage{graphicx}

\newcommand{\Notes}[1]{\captionsetup{position=below}\caption*{#1}}

\begin{document}

\begin{figure}
\caption{Actual caption}
\includegraphics{Example-image}
\Notes{Notes}
\end{figure}

\begin{figure}
\caption{Another caption}
\includegraphics{Example-image}
\Notes{Notes with full stop.}
\end{figure}

\end{document}

答案1

它就像加载一样简单amsthm,它定义一个\@addpunct命令,只有当它前面没有标点符号时才会插入它的参数。

\documentclass{scrartcl}
\usepackage{amsthm}% yes, that's it!
\usepackage{caption}
\usepackage{graphicx}

\makeatletter
\newcommand{\Notes}[1]{\captionsetup{position=below}\caption*{#1\@addpunct{.}}}
\makeatother

\begin{document}

\begin{figure}[htp]
\centering
\caption{Actual caption}
\includegraphics[width=4cm]{example-image}
\Notes{Notes}
\end{figure}

\begin{figure}[htp]
\centering
\caption{Another caption}
\includegraphics[width=4cm]{example-image}
\Notes{Notes with full stop.}
\end{figure}

\begin{figure}[htp]
\centering
\caption{Another caption}
\includegraphics[width=4cm]{example-image}
\Notes{Notes with exclamation mark!}
\end{figure}

\end{document}

在此处输入图片描述

答案2

下面的示例评估标题末尾的空间因子来检测之前是否有标点符号。

\documentclass[a5paper]{scrartcl}
\usepackage[paperheight=7cm]{geometry}% for smaller image in answer for TeX.SX
\usepackage{caption}
\usepackage{graphicx}

\newcommand{\Notes}[1]{\captionsetup{position=below}\caption*{#1}}

\makeatletter
\newcommand*{\RedefCaption}{%
  \let\org@caption\@caption
  \let\@caption\dot@caption
}
\def\dot@caption#1[#2]#3{%
  \dot@test@def\tmp@a{#2}%
  \dot@test@def\tmp@b{#3}%
  \org@caption{#1}[\tmp@a]\tmp@b
}
\newcommand*{\dot@test@def}[2]{%
  \begingroup
    % Set space factor to values larger than 1000 for punctuation characters
    \nonfrenchspacing
    % Set space factor to 1000 for upper case letter, which usually
    % have 999. This supports abbreviations: After an uppercase letter
    % the space factor is 999, a period would want it to set to 3000,
    % but TeX does not increase over 1000 in one step. Thus, the
    % space factor is 1000 after "A.".
    % Here, we do not want to support abbreviations, because
    % a period needs to be omitted, if there was a dot before,
    % regardless if the previous dot marks the end of the sentence
    % or belongs to an abbreviation.
    \count@=`\@\relax
    \@whilenum\count@<`\Z\do{%
      \advance\count@\@ne % \@ne = 1
      \sfcode\count@=\@m % \@m = 1000
    }%
    \sbox0{#2%
      \ifnum\spacefactor>\@m
        \global\let\dot@found=Y%
      \else
        \global\let\dot@found=N%
      \fi
    }%
  \endgroup
  \if\dot@found Y%
    \def#1{#2}%
  \else
    \def#1{#2.}%
  \fi
}
\AtBeginDocument{\RedefCaption}
\makeatother

\begin{document}

\begin{figure}
\caption{Actual caption}
% \includegraphics{Example-image}
\Notes{Notes}
\end{figure}

\begin{figure}
\caption{Another caption}
% \includegraphics{Example-image}
\Notes{Notes with full stop.}
\end{figure}

\end{document}

结果

答案3

\caption(您的要求对我来说并不完全清楚。我假设您希望在和的参数末尾使用句号(句号)\Notes。如果只需要\Notes,我相信您将能够弄清楚如何修改下面显示的代码。)

\caption这是一个基于 LuaLaTeX 的解决方案。它在和指令的参数末尾添加了缺失的句号(“句号”)\Notes。它的工作原理是在处理的早期阶段扫描输入,查找和的实例\caption{...}\Notes{...}检查参数;如果它们末尾缺少句号,则插入一个句号。

输入流的唯一要求是命令\caption及其\Notes各自的参数都在同一行。我相信这不构成约束限制。

在此处输入图片描述

%% Compile with LuaLaTeX
\documentclass{scrartcl}
\usepackage{caption}

\usepackage{luacode}
\begin{luacode*}
function add_period ( s )
  if string.find ( s , "\\caption%s-{" ) then
    s = string.gsub ( s , "(\\caption)%s-(%b{})", 
           function ( capt , argu )
              argu = string.sub ( argu , 2 , -2 )
              if not string.find ( argu , "%.%s-$" ) then 
                argu = argu .. "."
              end
              return ( capt .. "{" .. argu .. "}" )
           end )
  end
  if string.find ( s , "\\Notes%s-{" ) then
    s = string.gsub ( s , "(\\Notes)%s-(%b{})", 
           function ( capt , argu )
              argu = string.sub ( argu , 2 , -2 )
              if not string.find ( argu , "%.%s-$" ) then 
                argu = argu .. "."
              end
              return ( capt .. "{" .. argu .. "}" )
           end )
  end
  return ( s ) 
end
luatexbase.add_to_callback( "process_input_buffer" , add_period , "add_period" )
\end{luacode*}        

\newcommand{\Notes}[1]{\captionsetup{position=below}\caption*{#1}}

\begin{document}

\begin{figure}[ht!]
\caption {Actual caption}
\Notes{Notes}
\end{figure}

\begin{figure}[h!]
\caption {Another caption. } \Notes {Notes with full stop. }
\end{figure}

\end{document}

答案4

使用\@addpunct(egreg's) 显然比这个更好/更简单 (+1)。但为了完整性,使用该xstring包,您可以使用 检查某些字符串的结尾\IfEndWith

为简单起见,假设您只检查句号,而不检查其他标点符号,并且假设排序标题中没有标点符号,则宏可以是:

\documentclass[12pt]{article}
\usepackage{xstring,xifthen}
\newcommand\Note[2][]{
\ifthenelse{\isempty{#1}}{%
\IfEndWith{#2}{.}{\caption[#2]{#2}}{\caption[#2.]{#2.}}}{%
\IfEndWith{#2}{.}{\caption[#1.]{#2}}{\caption[#1.]{#2.}}}}
\begin{document}
\begin{figure}
\Note{Caption with period.}
\Note{Caption without period}
\Note[No]{No, without period}
\Note[Yes]{Yes, with period.}
\end{figure}
\listoffigures
\end{document}

姆韦

相关内容