8

I'm trying renew \texttt command with condition on ending . or ,. But after making this:

\usepackage{xstring}

\let\OldTexttt\texttt
\renewcommand{\texttt}[1]{%
    \OldTexttt{\hspace{0.4em}#1}%
    \IfEndWith{#1}{.}{}{\hspace{0.4em}}%    
}

Edit: not working example.

\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}

Error:

! 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.}}

Can anybody help what's wrong?

  • Please provide a minimal working example. Your code snippet works for me. – Marco Daniel Apr 4 '13 at 17:14
  • Edited original question. – Petr Přikryl Apr 4 '13 at 17:37
  • I see the issue is related to \section. I didn't test it. – Marco Daniel Apr 4 '13 at 17:45
7

here a solution trying expl3

\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}
10

The redefined \texttt is not robust. This can be done by using \DeclareRobustCommand and using package letltxmacro that takes care of LaTeX's internals of the original robust \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}

Result

Update: Also the logic in the macro is fixed to get 0.4em if the string does not end with . or ,.

5

This is a job for l3regex! Of course one should still save \texttt using \LetLtxMacro. However, I wouldn't use \hspace*: the additional space should disappear at a line break.

\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}

enter image description here

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.