4

I used the package embrac that the following question Upright parentheses in italic text let to.

However, If I define a simple environment, it will not work. See the example below. How can I fix this?

\documentclass{article}

\usepackage{embrac}

\begin{document}

\newcommand*{\TextA}{(\ldots) this is a citation from a famous author}
\newcommand*{\TextB}{\textup{(\ldots)} this is a citation from a famous author}

\textit{\TextA}\\
\textit{\TextB}\\
\TextA

\end{document}
cacamailg
  • 8,405
  • 3
    \expandafter\textit\expandafter{\TextA}. embrac doesn't make \textit expand its argument – cgnieder Aug 10 '15 at 14:26
  • BTW: \newcommand doesn't define an environment. That would be \newenvironment. – cgnieder Aug 10 '15 at 14:27
  • Yes. You are right. It was a misspelling from my side. I just wanted a command to illustrate the behaviour. – cacamailg Aug 10 '15 at 14:42
  • @clemens is it difficult/break something if internally you make embrac expand textit or emph? – cacamailg Aug 10 '15 at 14:45
  • 1
    Quite easy, actually. In an expl3 programming catcode regime: \cs_generate_variant:Nn \embrac_textit:nn {no} (same for \emph and \textsl) – cgnieder Aug 10 '15 at 14:51
  • I tried to change it using my MWE using your advices, but it didn't work as expected. – cacamailg Aug 10 '15 at 15:00
  • One way around this would be to define \def\mytextit#1{\expandafter\textit\expandafter{#1}} and then use \mytextit{\TextA}. In my tests this worked. – sgmoye Aug 10 '15 at 15:05
  • Since this doesn't tell me what exactly you've done and what your expectations were I can't really comment on that… – cgnieder Aug 10 '15 at 15:05
  • I have add: \ExplSyntaxOn \cs_generate_variant:Nn \embrac_textit:nn {no} \cs_generate_variant:Nn \embrac_emph:nn {no} \cs_generate_variant:Nn \embrac_textsl:nn {no} \ExplSyntaxOff – cacamailg Aug 10 '15 at 15:11
  • @cacamailg It doesn't suffice because the definition of \textit still uses the nn-variant. You must redefine \textit, too, see my answer. – cgnieder Aug 10 '15 at 15:13

1 Answers1

2

You need to expand \TextA before feeding it to \textit:

\expandafter\textit\expandafter{\TextA}

If you like you can redefine \textit in such a way that it expands its argument one time first:

\ExplSyntaxOn
\cs_generate_variant:Nn \embrac_textit:nn {no}

\RenewDocumentCommand \textit {sm}
  {
    \IfBooleanTF {#1}
      { \embrac_textit:no {*} {#2} }% using the new variant
      { \embrac_textit:no { } {#2} }% using the new variant
  }
\ExplSyntaxOff

(Correspondingly with \embrac_emph:nn for \emph and \embrac_textsl:nn for \textsl)

Note that this only expands the argument once. In \textit{x\TextA} \TextA still won't be expanded. You'd need an x-variant then (\edef-like expansion) but that might be dangerous… Maybe some \etextit for usage on own risk:

\ExplSyntaxOn
\cs_generate_variant:Nn \embrac_textit:nn {nx}

\NewDocumentCommand \etextit {sm}
  {
    \IfBooleanTF {#1}
      { \embrac_textit:nx {*} {#2} }
      { \embrac_textit:nx { } {#2} }
  }
\ExplSyntaxOff
cgnieder
  • 66,645
  • This actually works. Maybe you can add it to your package as an option like expandfirst and also the second variant, with an alert to users. – cacamailg Aug 10 '15 at 15:10
  • Strangely it works with the mininal example, but it is not working with the full document that I am working on. Any ideas? – cacamailg Aug 10 '15 at 15:29
  • @cacamailg No? I don't know your document and not your use case – cgnieder Aug 10 '15 at 15:31
  • The preamble is to big to add it here. I will try to investigate what is happening. Thanks – cacamailg Aug 10 '15 at 15:38