Estou tentando renovar \texttt
o comando com condição de término .
ou ,
. Mas depois de fazer isso:
\usepackage{xstring}
\let\OldTexttt\texttt
\renewcommand{\texttt}[1]{%
\OldTexttt{\hspace{0.4em}#1}%
\IfEndWith{#1}{.}{}{\hspace{0.4em}}%
}
Editar: exemplo não funcional.
\documentclass{article}
\usepackage{xstring}
\let\OldTexttt\texttt
\renewcommand{\texttt}[1]{%
\OldTexttt{\hspace{0.4em}#1}%
\IfEndWith{#1}{.}{}{\hspace{0.4em}}%
\IfEndWith{#1}{,}{}{\hspace{0.4em}}%
}
\begin{document}
\section{This is \texttt{section.}}
bbbb
\texttt{test}
\texttt{section.}
\texttt{section,}
\texttt{test}
aaa
\end{document}
Erro:
! Argument of \@sect has an extra }.
<inserted text>
\par
l.18 \section{This is \texttt{section.}}
?
! Emergency stop.
<inserted text>
\par
l.18 \section{This is \texttt{section.}}
Alguém pode ajudar o que há de errado?
Responder1
aqui uma solução tentandoexpl3
\documentclass[]{article}
\usepackage[T1]{fontenc}
\usepackage{beramono}
\usepackage{letltxmacro}
\LetLtxMacro\OldTexttt\texttt
\usepackage{xparse}
\ExplSyntaxOn
\cs_new:Npn \petr_extract_lasttoken:n #1
{
\tl_head:f { \tl_reverse:n { #1 } }
}
\cs_generate_variant:Nn \tl_if_eq:nnF {xnF}
\DeclareDocumentCommand \texttt { m }
{
\hspace*{0.4cm}
\OldTexttt{ #1 }
\tl_if_eq:xnF { \petr_extract_lasttoken:n {#1} } { . } {\hspace*{0.4cm}}
}
\ExplSyntaxOff
\begin{document}
foo \texttt{bar} foo
foo \texttt{bar.} foo
\section{This is \texttt{section.}}
bbbb
\texttt{test}
\texttt{section.}
\texttt{section,}
\texttt{test}
aaa
\end{document}
Responder2
O redefinido \texttt
não é robusto. Isso pode ser feito usando \DeclareRobustCommand
e usando o pacote letltxmacro
que cuida dos aspectos internos do LaTeX do robusto original \texttt
:
\documentclass{article}
\usepackage{xstring}
\usepackage{letltxmacro}
\LetLtxMacro\OldTexttt\texttt
\DeclareRobustCommand*{\texttt}[1]{%
\OldTexttt{\hspace{0.4em}#1}%
\IfEndWith{#1}{.}{}{%
\IfEndWith{#1}{,}{}{\hspace{0.4em}}%
}%
}
\begin{document}
\section{This is \texttt{section.}}
bbbb
[\texttt{test}]
[\texttt{section.}]
[\texttt{section,}]
[\texttt{test}]
aaa
\end{document}
Atualizar:Além disso, a lógica na macro é corrigida para obter 0.4em
se a string não terminar com.
ou ,
.
Responder3
Este é um trabalho para l3regex
! É claro que ainda se deve economizar \texttt
usando \LetLtxMacro
. No entanto, eu não usaria \hspace*
: o espaço adicional deveria desaparecer em uma quebra de linha.
\documentclass{article}
\usepackage[T1]{fontenc}
\usepackage{letltxmacro,xparse,l3regex}
\LetLtxMacro\latextexttt\texttt
\ExplSyntaxOn
\DeclareDocumentCommand \texttt { m }
{
\petr_spaced_texttt:n { #1 }
}
\cs_new_protected:Npn \petr_spaced_texttt:n #1
{
\hspace{0.4em}
\latextexttt{ #1 }
% Check if the string ends with a period or a comma
% \Z matches the end of the string
\regex_match:nnF { (\.|\,) \Z } { #1 } { \hspace{0.4em} }
}
\ExplSyntaxOff
\begin{document}
\tableofcontents
\section{This is |\texttt{section.}|}
bbbb
|\texttt{test}|
|\texttt{section.}|
|\texttt{section,}|
|\texttt{test}|
aaa
\end{document}