4

I have a LaTeX problem that I cannot solve by myself. I have a repeating arrangement of characters which I want to format automatically in LaTeX.

1H-NMR (400 MHz; DMSO-d6): \textdelta  12.41 (s, OH), 7.36-7.30 (m,
Cbz), 7.20 (t, J = 5.5 Hz, NH), 5.00 (s, Cbz-CH2), 3.48 (q, J = 13.4
Hz, N-CH2), 3.33 (t, J = 7.3 Hz, N-CH), 2.96 (q, J = 6.2 Hz, NH-CH2),
1.57 (td, J = 16.8, 9.0 Hz, CH2), 1.40 (d, J = 3.4 Hz, CH2), 1.30-1.24 (m, CH2).

Now I have to change all J to italic *J*. Of course I can do all these manually but I have quite a lot of these data sets and an automation would be very helpful. I thought of an new environment which changes all J to italic J. Is there any possibility? Can I check it with an if-then command? Like if="character"=J then italic else normal

I hope you can help me with that problem.

UPDATE:

thanks for all the help. I tried to understand all your suggestions and both work fine for one dataset. I now tried to define a new environment, which I can use for multiple datasets in my work.

\documentclass{article}
\begin{document}
\newenvironment{NMR2}
    {
        \def\dataset{
    }
    {
        }
        \xpatchcmd*{\dataset}{J}{\emph{J}}{}{} % making problems
        \dataset
    }

\begin{NMR2}
1H-NMR (400 MHz; DMSO-d6):  12.41 (s, OH), 7.36-7.30 (m,
Cbz), 7.20 (t, J = 5.5 Hz, NH), 5.00 (s, Cbz-CH2), 3.48 (q, J = 13.4
Hz, N-CH2), 3.33 (t, J = 7.3 Hz, N-CH), 2.96 (q, J = 6.2 Hz, NH-CH2),
1.57 (td, J = 16.8, 9.0 Hz, CH2), 1.40 (d, J = 3.4 Hz, CH2), 1.30-1.24 (m, CH2).
\end{NMR2}
\end{document}

and I tried the same for the second idea:

\documentclass{article}
\begin{document}
\newenvironment{NMR}
    {
    \def\oldJ{J} % \oldJ contains "J"
    \let\J=\oldJ % \J contains "J"
    \catcode`\J=13 % makes J an active character
    \defJ{\textit{\J}} % makes J italic
    }
    {
    \catcode`\J=11 %  makes J from an active character to a letter again
    }

\begin{NMR}
1H-NMR (400 MHz; DMSO-d6):  12.41 (s, OH), 7.36-7.30 (m,
Cbz), 7.20 (t, J = 5.5 Hz, NH), 5.00 (s, Cbz-CH2), 3.48 (q, J = 13.4
Hz, N-CH2), 3.33 (t, J = 7.3 Hz, N-CH), 2.96 (q, J = 6.2 Hz, NH-CH2),
1.57 (td, J = 16.8, 9.0 Hz, CH2), 1.40 (d, J = 3.4 Hz, CH2), 1.30-1.24 (m, CH2).
\end{NMR}
\end{document}

Both examples making problems and do not work. What am I doing wrong?

  • 1
    welcome to TeX.SE! – Nils L Jun 12 '13 at 17:54
  • Since you are using LaTeX, you are creating a document. Are you hoping to change all "J" characters throughout the document, or merely all "J" characters that are part of these data streams fed into your document. The first might involve some sort of font redefinition, whereas the second would involve string manipulation. Also, are these files normal text, or do they contain \, & % characters, etc. – Steven B. Segletes Jun 12 '13 at 18:13
  • @Volker: what would be good to know is if your data set is more of a static or a dynamic one. For if it's not going to change in the near future, why not just do a search and replace using your editor? J to \emph{J}. – Nils L Jun 12 '13 at 18:21
  • @everyone: I was thinking of storing Volker's data sets in a command and then use etoolbox's patching features. Something like \patchcmd{\dataset}{J}{\emph{J}}{}{}, but I realized this only works on the 1st occurence. Is there a way to change that behaviour? – Nils L Jun 12 '13 at 18:28
  • @NilsL You could use regexpatch and \xpatchcmd*{\dataset}{J}{\emph{J}}{}{}, because \xpatchcmd* replaces all occurrences. – egreg Jun 12 '13 at 19:46
5

You can't define an environment like you propose in the edit. You can, though, absorb the environment's contents and then do a replacement.

I can propose three methods, all based on the environ package.

Method 1 (with regexpatch)

The environ package defines NMR2 in such a way that the environment's contents is stored in \BODY; then we can use \xpatchcmd* to replace all occurrences of J with \textit{J}.

\documentclass{article}
\usepackage{regexpatch,environ}

\NewEnviron{NMR2}{%
  \xpatchcmd*\BODY{J}{\textit{J}}{}{}%
  \BODY}

\begin{document}

\begin{NMR2}
1H-NMR (400 MHz; DMSO-d6):  12.41 (s, OH), 7.36-7.30 (m,
Cbz), 7.20 (t, J = 5.5 Hz, NH), 5.00 (s, Cbz-CH2), 3.48 (q, J = 13.4
Hz, N-CH2), 3.33 (t, J = 7.3 Hz, N-CH), 2.96 (q, J = 6.2 Hz, NH-CH2),
1.57 (td, J = 16.8, 9.0 Hz, CH2), 1.40 (d, J = 3.4 Hz, CH2), 1.30-1.24 (m, CH2).
\end{NMR2}
\end{document}

Method 2 (with l3regex)

Since regexpatch is based on l3regex, one could directly use the regex module.

\documentclass{article}
\usepackage{l3regex,environ}

\ExplSyntaxOn
\NewEnviron{NMR2}
 {
  \regex_replace_all:nnN { J } { \c{textit}\cB\{ J \cE\} } \BODY
  \BODY
 }
\ExplSyntaxOff

\begin{document}

\begin{NMR2}
1H-NMR (400 MHz; DMSO-d6):  12.41 (s, OH), 7.36-7.30 (m,
Cbz), 7.20 (t, J = 5.5 Hz, NH), 5.00 (s, Cbz-CH2), 3.48 (q, J = 13.4
Hz, N-CH2), 3.33 (t, J = 7.3 Hz, N-CH), 2.96 (q, J = 6.2 Hz, NH-CH2),
1.57 (td, J = 16.8, 9.0 Hz, CH2), 1.40 (d, J = 3.4 Hz, CH2), 1.30-1.24 (m, CH2).
\end{NMR2}
\end{document}

Method 3 (with \scantokens)

\documentclass{article}
\usepackage{environ}

\NewEnviron{NMR2}{%
  \begingroup\lccode`\~=`\J
  \lowercase{\endgroup\def~}{\textit{J}}%
  \catcode`\J=\active\scantokens\expandafter{\BODY\relax}}

\begin{document}

\begin{NMR2}
1H-NMR (400 MHz; DMSO-d6):  12.41 (s, OH), 7.36-7.30 (m,
Cbz), 7.20 (t, J = 5.5 Hz, NH), 5.00 (s, Cbz-CH2), 3.48 (q, J = 13.4
Hz, N-CH2), 3.33 (t, J = 7.3 Hz, N-CH), 2.96 (q, J = 6.2 Hz, NH-CH2),
1.57 (td, J = 16.8, 9.0 Hz, CH2), 1.40 (d, J = 3.4 Hz, CH2), 1.30-1.24 (m, CH2).
\end{NMR2}
\end{document}

Output for all three methods

enter image description here

  • Hi. I tried Method 1 which works great. Thank you very much for all your help. – Volker Jun 13 '13 at 13:04
1

(With my definition of \textdelta):

\documentclass{article}

\begin{document}

\def\textdelta{\mbox{$\delta$}}

\def\oldJ{J}
\let\J=\oldJ

\catcode`\J=13
\defJ{\textit{\J}}
1H-NMR (400 MHz; DMSO-d6): \textdelta  12.41 (s, OH), 7.36-7.30 (m,
Cbz), 7.20 (t, J = 5.5 Hz, NH), 5.00 (s, Cbz-CH2), 3.48 (q, J = 13.4
Hz, N-CH2), 3.33 (t, J = 7.3 Hz, N-CH), 2.96 (q, J = 6.2 Hz, NH-CH2),
1.57 (td, J = 16.8, 9.0 Hz, CH2), 1.40 (d, J = 3.4 Hz, CH2), 1.30-1.24 (m, CH2).

\catcode`\J=11 %  an attempt to final cleaning

\end{document}

enter image description here

Warning: Side effects possible, if additional packages are loaded.

  • 1
    \edef J{\noexpand\textit{\string J}} would avoid \oldJ – egreg Jun 12 '13 at 19:43
  • @egreg It is a very good suggestion. – Przemysław Scherwentke Jun 12 '13 at 21:12

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.