tex >> \newcommand problem

by enjoyfate » Fri, 25 Jul 2008 23:04:10 GMT

Using fancyvrb package, the code below can make a box arrow the text.
\begin{Verbatim}[frame=single]
$ ls
\end{Verbatim}
but every time input so many characters, it's boring. I wanna to turn
it to \bat{$ls } by using \newcommand
so I write codes below
\newcommand{\bat}[1]{\begin{Verbatim}[frame=single]
#1
\end{Verbatim}
but can't compile pass. How to archive the purpose.
Any hints would be appreciated


Similar Threads

1. \newcommand problem

On Jul 25, 11:04m,  XXXX@XXXXX.COM  wrote:
> Using fancyvrb ackage, the code below can make a box arrow the text.
> \begin{Verbatim}[frame=single]
> $ ls
> \end{Verbatim}
> but every time input so many haracters, it's boring. I wanna to turn
> it to \bat{$ls } by sing newcommand
> so I write codes below
> \newcommand{\bat}[1]{\begin{Verbatim}[frame=single]
> #1
> \end{Verbatim}
> but can't compile pass. How to archive the purpose.
> Any hints would be appreciated

You'll be hard-pressed to make this work, unfortunately.  The reason
is that (while I'm not actually familiar with fancyvrb, I can
speculate) in general, verbatim modes work by changing the way TeX
reads the characters (technically, the "category codes") that are
inside the environment.  In particular, it needs to sanitize the $ so
that TeX doesn't read it as a math shift but rather as a plain
letter.  But when you pass text as an argument to a macro, as in
\bat{$ls}, TeX has already read the entire argument and decided what
type of character everything is, so there's no chance for the verbatim
to work its magic.  So the (not so) short answer is "don't do that"
and just use the verbatim environment like it's meant to be used.

But there's still hope, on two conditions.  IF it's just the '$'
giving you trouble, AND if you never use '$' as a math shift (you can
avoid it by using \(...\) instead), then you can just change the
catcode yourself, by inserting
  \catcode`\$=12
in your document.  But at this point, the Verbatim environment isn't
really doing much anymore so you may be able to replace it with
something less potent.

You can, in fact, do this trick with any special character as long as
you don't ever use it (#, %, &, ~, ^, and _ should all work if you
don't need them elsewhere (this includes comments)...  I don't
recommend trying \ unless you really know what you're doing).

Cheers,
steve

2. newcommand problem

3. \newcommand vs. \newcommand*

I have found a piece of latex code (in the texmate package) like this:

\def\wname{}
\def\bname{}
\let\@welo\relax
\let\@belo\relax
\newcommand\whitename[1]{\def\wname{#1}}
\newcommand\blackname[1]{\def\bname{#1}}
\newcommand*\whiteelo[1]{\def\@welo{(#1)}}
\newcommand*\blackelo[1]{\def\@belo{(#1)}}

What is the difference between using \newcommand* and \let...\relax
vs. \newcommand and \def?

4. LaTeX: using \newcommand inside a \newcommand

5. \newcommand with \bytefield problems

Forgive me if this has been answered - I haven't been able to find any
solutions on here or the internets.

I am constantly using the same formatting of a bytefield/bitfields to
describe the MIPS instruction set in my own words.  Because of this I
would like to create a new command called \macineCode that takes 6
arguments.

Currently I have:

\newcommand{\machineCode}[6]{\begin{bytefield}{32}
	\bitheader[b]{0,5,6,10,11,15,16,20,21,25,26,31}\\
	\bitbox{6}{#1} &\bitbox{5}{#2} &
	 \bitbox{5}{#3} & \bitbox{5}{#4} &
	  \bitbox{5}{#5} & \bitbox{6}{#6} \\
\end{bytefield}
}


When trying to typeset it, I get the following error twice on the line
that I call \machineCode:
Extra alignment tab has been changed to \cr. \endtemplate


I am trying to make it look like this:

\begin{tabular}{lrlrlrlrlrlr}
	31&26&25&21&20&16&15&11&10&6&5&0 \\
	\hline
	\multicolumn{2}{|c|}{\textbf{000000}}&
	\multicolumn{2}{c|}{\textbf{Rs}}&
	\multicolumn{2}{c|}{\textbf{Rt}}&
	\multicolumn{2}{c|}{\textbf{Rd}}&
	\multicolumn{2}{c|}{\textbf{00000}}&
	\multicolumn{2}{c|}{\textbf{100000}} \\
	\multicolumn{2}{|c|}{opcode}&
	\multicolumn{2}{|c|}{}&
	\multicolumn{2}{|c|}{}&
	\multicolumn{2}{|c|}{}&
	\multicolumn{2}{|c|}{shamt}&
	\multicolumn{2}{|c|}{func}   \\
	\hline
	\multicolumn{2}{c}{6}&
	\multicolumn{2}{c}{5}&
	\multicolumn{2}{c}{5}&
	\multicolumn{2}{c}{5}&
	\multicolumn{2}{c}{5}&
	\multicolumn{2}{c}{6} \\
\end{tabular}

6. Problem with newcommand

7. Problems with \newcommand in TeXML

For the attention, in particular, of Oleg Paraschenko.

Oleg Paraschenko wrote:
>> Could you please provide more details, then I'll fix the problem.

Dear Sir,
Ref your message to comp.text.tex, dated Wed 05 July 2006


First, I am sorry not to have replied sooner. I only 
noticed yesterday that you had replied to my message.

Secondly, my comments are much less important in the new
version of texml, with the attribute 'escape' to
the <TeXML> element.

I think my problems with obtaining \newcommand{...}
in TeXML format were mainly related to the use
of <cmd> embedded in an outer <cmd>.

To illustrate these difficulties, I have put 
a story together as a
(slightly fictional) version of me climbing
the texml learning curve. Here it is, with
the failed attempts commented out.


+++++++ snip -------------------------- ++++++++++++++


<?xml version="1.0" encoding="utf-8" ?>
<TeXML>
<TeXML escape="0">
\documentclass[a4paper]{article}
\usepackage[latin1]{inputenc}
\usepackage[T1]{fontenc}
\newcommand{\Fred}[1]{Fred said \emph{#1}{}}
</TeXML>

<!-- Once upon a time, I tried to create a very similar
definition to \Fred, without the attribute escape="0" -->

<!-- Attempt 1, to me the most natural. Does not work.
   I used <cmd ..> for both the command name, and in its definition.
   - ->
<cmd name="newcommand">
  <parm><cmd name="William"/></parm>
  <opt>1</opt>
  <parm>Dick said <cmd name="emph"><spec cat="parm" />1</cmd></parm>
</cmd>
-->


<!-- Attempt 2. Does work.
   Does not use <cmd ..> in either position -->
<cmd name="newcommand"><parm><spec cat="esc" />Harry</parm>
<opt>1</opt>
<parm>Harry said, <TeXML ligatures="1">``<spec
cat="parm" />1''</TeXML></parm>
</cmd>

<!-- Attempt 3. Does not work.
   Builds the command name using
     <spec cat="esc"/>commandname
   Uses <cmd ..> within the definition.
- -> 
<cmd name="newcommand"><parm><spec cat="esc"/>Jim</parm>
<opt>1</opt>
<parm>Jim said <cmd name="emph"><spec cat="parm" />1</cmd></parm>
</cmd>
-->
   

<!-- Attempt 4. This version   works ... but what a tedious error-prone
strategy!
   build your own \emph command using three sub-structures: 
        i.   <spec cat="esc">emph
        ii.  <spec cat="bg"/>....<spec cat="eg"/>
        iii. <spec cat="parm">1
-->
<cmd name="newcommand"><parm><spec cat="esc"/>John</parm>
<opt>1</opt>
<parm>John said <spec cat="esc"/>textsc
<spec cat="bg"/>
<spec cat="parm" />1
<spec cat="eg"/>
</parm>
</cmd>


<!-- Attempt 5. Does not work, but it does get as far as a TeX file.
      ... it puts empty braces after \Dick, which makes the TeX invalid.
   Uses <cmd ..> for the command name only.
   The definition is the one already tested in Attempt 4.

<cmd name="newcommand">
  <parm><cmd name="Dick"/></parm>
  <opt>1</opt>
  <parm>Dick said <spec cat="esc"/>emph<spec cat="bg"/><spec
cat="parm" />1<spec cat="eg"/></parm>
</cmd>
-->




<env name="document">

<cmd name="Fred"><parm>Hi, there!</parm></cmd><cmd name="par" />
<!-- <cmd name="William"><parm>Hi, there!</parm></cmd><cmd name="par" /> -->
<cmd name="Harry"><parm>Good morning.</parm></cmd><cmd name="par" />
<!-- <cmd name="Jim"><parm>Hi, there!</parm></cmd><cmd name="par" />  -->
<cmd name="John"><parm>G'day to you!</parm></cmd><cmd name="par" />
<!-- <cmd name="Dick"><parm>Hi, there!</parm></cmd><cmd name="par" /> -->
</env>
</TeXML>

8. Problem defining \newcommand