Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

Assume there is a line including some single letter like A, b, C, etc. and the letters separate by only one white space, which ASCII code is 32, say,

D B g A

I want the content for a \def's arguments, For example,

\def\mydef#1 #2 #3 #4{\textbf{#1}\textit{#2}\textsc{#3}\textsl{#4}}

the

\mydef{D B g A}

output bold D, italic B, smallcap g, and slanted A. Other situations export a warning message. Note: the \r\n (ASCII code 13) or \n (ASCII code 10) be seen as ending of \mydef.

share|improve this question
    
Actually, neither \r\n nor \n is what your \mydef sees, since TeX converts all the various system-dependent newline indicators into its internal one ^^M (which I believe is the same ASCII code as \n, however). –  Ryan Reich Apr 28 '11 at 13:57
add comment

2 Answers

up vote 6 down vote accepted
\documentclass{article}
\def\mydef#1 #2 #3 #4{\textbf{#1}\textit{#2}\textsc{#3}\textsl{#4}}
\def\myDef#1{\expandafter\myDefi#1}
\def\myDefi#1 #2 #3 #4{\textbf{#1}\textit{#2}\textsc{#3}\textsl{#4}}
\begin{document}
\mydef A B C D foo

\myDef{A B C D} foo
\end{document}
share|improve this answer
    
Nice solution! Thanks. –  Stufazi Hoqckt Apr 28 '11 at 8:44
    
What's the \expandafter for? The #1 will be "expanded" before \myDefi anyway, by the macro substitution rules, so it looks like you want to expand argument #1 of \myDefi but not the others. –  Ryan Reich Apr 28 '11 at 13:59
    
in this case it has no meaning, but it doesn't hurt and I use it by default, because I do not really know what arguments users may have in mind. –  Herbert Apr 28 '11 at 14:55
add comment

\mydef{D B g A} is reading {D B g A} as first argument because of the { }. It then looks for the #2 #3 #4 part afterwards and raises an error if there is no such input. You need to either write directly \mydef D B G A or use a second macro like that inside \mydef, which is more often used.

\def\mydef#1{\mydefb#1}
\def\mydefb#1 #2 #3 #4{\textbf{#1}\textit{#2}\textsc{#3}\textsl{#4}}

You might want to add a trailing space to ensure that all input of \mydef is processed by \mydefb, otherwise if the last part is more than one letter the rest is just typeset after \mydefb:

\def\mydef#1{\mydefb#1 }
\def\mydefb#1 #2 #3 #4 {\textbf{#1}\textit{#2}\textsc{#3}\textsl{#4}}
share|improve this answer
    
Thanks your detailed explain. But Herbert is earier so I get tick to him. Too hard do the choice! –  Stufazi Hoqckt Apr 28 '11 at 8:51
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.