如果 '\paragraph' 的参数没有以标点符号结尾,则自动添加最后一个句号

如果 '\paragraph' 的参数没有以标点符号结尾,则自动添加最后一个句号

\paragraph如果 的参数不以.?或结尾,我希望自动添加最后一个句点!

解决方案已提供在每个段落标题后添加一个句号但这要求最后一个字母不是大写。如何处理一般情况?

\documentclass{article}

\usepackage{xparse}
\usepackage{amsthm} % for \@addpunct

\makeatletter
\let\latexparagraph\paragraph
\RenewDocumentCommand{\paragraph}{som}{%
  \IfBooleanTF{#1}
    {\latexparagraph*{#3}}
    {\IfNoValueTF{#2}
       {\latexparagraph{\maybe@addperiod{#3}}}
       {\latexparagraph[#2]{\maybe@addperiod{#3}}}%
  }%
}
\newcommand{\maybe@addperiod}[1]{%
  #1\@addpunct{.}%
}
\makeatother

\begin{document}

    
\paragraph{This is the title} And some text follows.

\paragraph{This is the title.} And some text follows.

\paragraph{Is this is a title?} And some text follows.

\paragraph{This is the title X} And some text follows.

\paragraph{This is the title X.} And some text follows.

\paragraph{Is this is a title X?} And some text follows.
    
\end{document}

在此处输入图片描述

答案1

您可以使用正则表达式。在这里我检查参数是否以“!”、“?”或“。”结尾,如果不是,则添加一个句点。

\documentclass{article}

\NewCommandCopy\latexparagraph\paragraph
\RenewDocumentCommand{\paragraph}{sO{#3}m}{%
  \IfBooleanTF{#1}
    {\latexparagraph*{\maybeaddperiod{#3}}}
    {\latexparagraph[#2]{\maybeaddperiod{#3}}}%
}

\ExplSyntaxOn
\NewDocumentCommand{\maybeaddperiod}{m}
 {
  \userninefourtwoninethree_maybeaddperiod:n { #1 }
 }

\cs_new_protected:Nn \userninefourtwoninethree_maybeaddperiod:n
 {
  \regex_match:nnTF { .*[\!\?\.] \Z } { #1 } { #1 } { #1. }
 }
\ExplSyntaxOff

\begin{document}

    
\paragraph{This is the title} And some text follows.

\paragraph{This is the title.} And some text follows.

\paragraph{Is this is a title?} And some text follows.

\paragraph{This is the title X} And some text follows.

\paragraph{This is the title X.} And some text follows.

\paragraph{Is this is a title X?} And some text follows.
    
\end{document}

在此处输入图片描述

\@无论如何,如果您养成在任何非缩写的尾随大写字母后添加的习惯,那么以前的解决方案就会有效。

答案2

这是一个基于 LuaLaTeX 的解决方案。它通过检查参数\paragraph并利用 Lua 强大的findgsubsub字符串函数来工作。如果字符串末尾没有标点符号,则插入句点。作为(可能很小的)奖励,此解决方案适用于 和\paragraph指令\paragraph*

在此处输入图片描述

% !TEX TS-program = lualatex
\documentclass{article}

\usepackage{luacode} % for 'luacode*' environment
\begin{luacode*}
function doit ( u , v ) -- aux. function that does most of the work
  v = v:sub ( 1, -2 )        -- strip off closing curly brace
  v = v:gsub ( "%s+$" , "" ) -- strip off any trailing whitespace
  w = v:sub ( -1 )           -- retrieve last char in 'v' string
  if ( w=="." or w=="?" or w=="!" ) then
  else
    v = v.."." -- add period to end of 'v' string
  end
  return u .. v .. "}" -- add back the closing curly brace
end
function maybeaddperiod ( s )      -- the main function
  if s:find ( "\\paragraph" ) then -- we need to do something...
    return s:gsub ( "(\\paragraph%*?)%s*(%b{})" , doit )
  end
end
\end{luacode*}
% Add the Lua function to LuaTeX's "process_input_buffer" callback:
\AtBeginDocument{\directlua{luatexbase.add_to_callback (
   "process_input_buffer" , maybeaddperiod, "maybeaddperiod" )}}

\begin{document} 
%% Insert period at end of arg of "\paragraph"
\paragraph{This is the title } And some text follows.
\paragraph{This is the title X} And some text follows.
\paragraph*{This is the title } And some text follows.

\bigskip
%% Don't insert period
\paragraph{This is the title. } And some text follows.
\paragraph{Is this the title?} And some text follows.
\paragraph{This is the title! } And some text follows. 
\end{document}

相关内容