Redefining Commands with Parameters using \newenvironment - latex

Pursuant to this question:
Redefining Commands in a New Environment
How does one redefine (or define using \def) a macro that uses parameters? I keep getting an illegal parameter definition in \foo error. Since I require custom delimiters, I can't use \newcommand or \renewcommand.
A general form of my code looks like this:
\newenvironment{foo}{%
...spacing stuff and counter defs...
\def\fooitem#1. #2\endfooitem{%
...stuff...
}
\def\endfooitem{endfoo}
}
{...after material (spacing)...}
This must be possible. Right now I'm using plain-TeX definitions (as I mentioned in the question above) but I'd really like to be consistent with the LaTeX system.

You need to double the # characters for every nested definition. Internally, a \newcommand or a \newenvironment is calling \def.
\newenvironment{foo}{%
...
\def\fooitem##1. ##2\endfooitem{%
...
Besides that, this is the way to do what you're trying to do; there is no pure-LaTeX method to define a macro with delimited arguments.

Related

Change format of inline code evaluation in org-mode's LaTeX-export

I have a code block in an org document
#+NAME: result_whatever
#+BEGIN_SRC python :session data :results value :exports none
return(8.1 - 5)
#+END_SRC
which I evaluate inline:
Now, does this work? Let's see: call_result_whatever(). I'd be surprised ...
When exporting to LaTeX, this generates the following:
Now, does this work? Let's see: \texttt{3.1}. I'd be surprised \ldots{}
However, I don't want the results to be displayed in monospace. I want it to be formatted in "normal" upright font, without any special markup.
How can I achieve this?
You should be able to get it work using the optional header arguments which can be added to call_function().
I don't have LaTeX installed on this system so can't fully test the outputs to ensure they come out exactly as desired, I'm using the plain text output to compare instead. However you can use the following syntax as part of your call to modify the results.
Now, does this work? Let's see call_results_whatever()[:results raw].
I'd be surprised ...
Without the [:results raw] the output to Plain Text (Ascii buffer) is Let's see `3.0999999999999996'.. With the added results it becomes Let's see 3.0999999999999996.
For full details of the available results keywords as well as other optional header arguments for the inline blocks please see Evaluation Code Blocks and Results arguments.
this is 5 years later. apparently in org-mode 8.2 or so, a new variable was introduced (documenting in "Evaluating Code Blocks" in the org-mode manual, but this from etc/ORG-NEWS in the source tree):
*** New option: org-babel-inline-result-wrap
If you set this to the following
: (setq org-babel-inline-result-wrap "$%s$")
then inline code snippets will be wrapped into the formatting string.
so, to eliminate \texttt{}
(setq org-babel-inline-result-wrap "%s")
The problem of this type can be solved in two ways:
1: Easy does it:
A plain query-replace on the exported buffer.
Once you're in the LaTeX buffer,
beginning-of-buffer or M-<
query-replace or M-%
enter \texttt as the string that you want to replace
enter nothing as the replacement
continue to replace each match interactively
with y/n or just replace everything with !
2: But I wanna!
The second way is to nag the org-mode mailing list into
implementing a switch or an option for your specific case.
While it's necessary sometimes, it also produces a system
with thousands of switches, which can become unwieldy.
You can try, but I don't recommend.

Can I get lex to put out a yylex() function with a different name?

I want to have two lexers in one project, and I don't want to run into problems with having multiple yylex functions in the build. Can I make lex output with a different prefix?
You can use the -Pprefix parameter for flex in your makefile. Using flex -Pfoo you would effectively prefix all yy generated functions. Have a look at the manual page for further details.
flex lets you do that. Just define the YY_DECL macro. Dunno about actual Unix(tm) lex(1) though.
You could build a C++ lexer. This means all the state information is held in an object.
Then it is just a matter of using the correct object!

How to prevent LaTeX from hyphenating words containing a dash?

I would like to globally prevent LaTeX from hyphenating 'Objective-C'. I am aware of the \hyphenation command, but I don't see how I can make use of it. If I pass 'Objective-C' to this command, the dash will be treated as a hint to hyphenate the word there.
One solution I found is wrapping Objective-C into an mbox each time I use it. However, the document I am writing contains this name a lot, and wrapping it into an mbox each time is ugly (as is defining a command and using this over and over again in the source code).
Why is defining a new command ugly? It's how \LaTeX\ defines itself.
\def\ObjectiveC{\mbox{Objective-C}}
Use \nobreakdash. That's what LyX produces when I insert a nonbreakingdash and convert it to tex.
As suggested here, you could define a command like this:
\newcommand\dash{\nobreakdash-\hspace{0pt}}
and use it like this
Consider the $n$\dash dimensional manifold ...
Also, you could use the babel package and use "~ as a protected hyphen. I'm not sure if using babel is advisable when writing in english, though.

LaTeX: Redefining the citation macro temporarily

I'm new to latex, but it seems you can temporarily redefine some commands? The problem I have is that normally the citation is (SMITH, 2000). But at times, I'd like to have Smith (2000) instead. Anyway I can temporarily redefine it, then use the standard version again after that "block" of code?
Enclose the local definition in braces ({}).
\newcommand\foo{FOO!}
\foo
{\renewcommand\foo{BAR?}\foo\foo}
\foo
This will generate something like:
FOO!BAR?BAR? FOO!
This can be done inside macro definitions, too - just make sure you add the extra braces:
\newcommand\newfoo{{\renewcommand\foo{BAR?}\foo}}
Hope this helps.
You should already have \citep and \citet commands that should do what you want, there's no reason to redefine the macros yourself.

wrap LaTeX command in environment

How can I wrap a LaTeX command in an environment? In essence, how can I turn \somecommand{contents} into \begin{somecommand} contents \end{somecommand}? I have tried the obvious in creating a new environment as such:
\newenvironment{somecommand}[0]{
\somecommand{
}
{
}
}
but this causes problems with the curly brackets. Let me give a more concrete example. Say that you want to create the environment very-important and you want to use the command emph to accomplish this. An straightforward (but incorrect) solution would be to write something as
\newenvironment{very-important}[0]{
\emph{
}
{
}
}
The problem here is that the command works with the information that is found inside the environment, so it is not one of the opening commands of the environment, nor is it a closing command of the environment. The question is then: how can you do this?
This can be done with the environ package as follows:
\usepackage{environ}
...
\NewEnviron{very-important}{\emph{\BODY}}
\BODY contains the body of the environment, and environments may be nested. See the documentation for more details.
It seems that now I can guess what is the question.
\newenvironment{very-important}{\startimportant}{}
\def\startimportant#1\end{\emph{#1}\end}
\begin{very-important}
Something
\end{very-important}
This solution works well.
But IMHO it is bad idea to wrap all text in the environment. Why?
There are two ways to do something with the text.
For example, you want to change the font and use italic.
The first method. \textit{sentence written in italics}
The second method. {\it sentence written in italics\/}
What is the difference? The thing is that first method use the second one.
\it macro changes the font and the brace } changes it back.
\textit macro reads the full argument, changes the font and inserts the argument again:
\textit is defined roughly as follows (not exactly).
\def\texit#1{{\it#1\/}}
The first method is always doing extra work. It reads the argument twice.
Almost always, you can make changes and then you can everything back.
Eventually why do you use the environment? Use macros.
\veryimportant{
Any thought
}
A Simpler way could be:
\newenvironment{somecommand}[0]{
\somecommand\bgroup
}
{
\egroup
}
Explanation: \bgroup works like { and \egroup works like }.
New environment somecommand defines the macro \somecommand.
You can not use macro with the same name \somecommand inside.
Moreover you should write
\newenvironment{name}{openning command}{closing commands}
rather than
\newenvironment{somecommand}[0]{ \somecommand{ } { } }
You obviously have a problem with closing commands.
define command
\newcommnad{eqn}{1}{\begin{equation}#1\end{equation}}
will change
\eqn{x^2=y}
to
\begin{equation}
x^2=y
\end{equation}
I think

Resources