braces for Math in LaTeX - latex

Is there any convenient way to type in braces in Math in Latex?
Especially when I have already typed in a lot of "(" and ")" instead of "\left(" and "\right)", how can I do a quick replacement? similarly for "{" and "}" and "[" and "]"?
Is it important to make such changes? I am writing a thesis.
Thanks and regards!
BTW: I mainly edit latex file in Texmaker and I know how to use emacs. If there is other editor that can do such replacement in a quick way, please let me know.
EDIT:
If there is nothing taking more than one line height inside the braces, is there any difference in their appearances between using \left and \right and not using? Is it still necessary to use \left and \right ?

The point of the \left( \right) syntax is to have brackets automatically resize to fit their content.
A search/replace is a simple way to correct your brackets (M-% ( RET \left( RET in emacs for example, which will let you check each occurrence), or your editing environment might have a quick way to type them.

Assuming that you don't want to replace ('s with \left('s in your source files, you can try this hack:
\def\oparen{\left(} \def\cparen{\left)}
{\catcode`\(=\active \gdef({\oparen} \catcode`\)=\active \gdef){\cparen}}
\everymath={\catcode`\(=\active \catcode`\)=\active}
But this will probably break some of the macros using parentheses.
The same trick possible with {/} (use with extreme care!) and [/].

Related

How do I use \renewcommand to get BACK my greek letters?

I'm a LaTeX newbie, but I've been doing my homework, and now I have a question that I can't seem to find the answer to.
I create the definition of an equation, let's just say it's this one:
The potential is characterized by a length $\sigma$ and an energy $\epsilon$.
In reality, this equation is more complex, which is why I wanted to try a shortcut. If my equation were this simplistic, I wouldn't try my substitution technique.
I use the \renewcommand to save me some time:
\renewcommand{\sigma}{1}
And this works fabulously and will replace all instances of sigma with 1. Unfortunately though, since \sigma has a global scope, I need to reset it.
I tried a couple different ways:
Attempt 1: -deadlock due to circular reference?
\newcommand{\holdsigma}{\sigma}
\renewcommand{\sigma}{1}
The potential is characterized by a length $\sigma$ and an energy $\epsilon$.
\renewcommand{\sigma}{\holdsigma}
I would think to reset the command, it should look something like this:
\renewcommand{\sigma}{\greek{\sigma}}
but that obviously hasn't worked out for me.
Any idea about how the greek letters are originally defined in the language?
I have to admit that I don't understand why you want to do what you're asking, but this should work:
\documentclass{article}
\begin{document}
Before redefinition, \verb|\sigma| looks like $\sigma$.
% Copy the current definition of \sigma to \oldsigma
\let\oldsigma\sigma
% Redefine \sigma to be '1'
\renewcommand{\sigma}{1}
After redefinition, \verb|\sigma| looks like $\sigma$.
You can still use \verb|\oldsigma| if you want to use the original definition $\oldsigma$.
% Restore the original definition of \sigma
\let\sigma\oldsigma
Now \verb|\sigma| is back to its normal appearance $\sigma$.
\end{document}
To find out how \sigma or any other command is originally defined, you can use \show\sigma. (The answer is that \sigma is defined as \mathchar"11B.) You can type this either in your document itself — compilation will pause and you can type Enter after reading the reply — or you can type this in TeX/LaTeX's interactive mode.
Example with a document:
\documentclass{article}
\begin{document}
What is $\sigma$? % Prints "What is σ" in the DVI/PS/PDF.
\show\sigma % Prints "> \sigma=\mathchar"11B." in the compilation.
Now that we know, let us redefine it.
\renewcommand{\sigma}{1}
Now it is: $\sigma$. % Prints "Now it is: 1." in the DVI/PS/PDF.
OK, let's go back.
\renewcommand{\sigma}{\mathchar"11B}
We again have: $\sigma$. %Prints "We again have: σ." in the DVI/PS/PDF.
\end{document}
Or else at the command prompt, type latex, then type \relax, then type \show\sigma, read what it says, then type x to exit.

Implicit linebreaks in LaTeX equations

I wonder if there is any way to invert the way the LaTeX interprets linebreaks in equations? E.g., I dont want to insert them explicitly like,
\begin{gather}
x = y \\
a = c
\end{gather}
, but implicitly like,
\begin{gather}
x = y
a = c
\end{gather}
Thanks.
This is against the intention of TeX’s author, who believed that math must be typeset by hand. I tried obeylines, but to no avail. I guess it’s possible by making new line active, but you should ask the cracks over at Stack Exchange, a branch of Stack Overflow for TeX and LaTeX.
The breqn package will automatically insert linebreaks in equations when the line is full. I don't know of anything that will do break as you ask. If it is a big deal you could use perltex to define a macro that would do it for you. I will try to mock one up as an example.

Multiset notation in LaTeX

Does anyone know how to make (nice looking) double bracket multiset notation in LaTeX, i.e something like (\binom{n}{k}) where there are two outer brackets instead of 1 as in binomial? You can see an example of what I mean in http://en.wikipedia.org/wiki/Multiset under the heading "Multiset coefficients" with the double brackets.
In Wikipedia they typeset it as:
\left(\!\!{n\choose k}\!\!\right)
but although this works well for LaTeX in maths mode, with inline equations the outer bracket becomes much larger than the inner bracket.
I have also tried using
\genfrac{((}{))}{0pt}{}{n}{k}
but it has an error with the double brackets.
I am using \binom as well in my document, so I would like the bracket sizes to be similar for \binom and \multiset.
You can explicitly specify the size of the brackets via
\big( \Big( \bigg( or \Bigg(
Then use \! for negative space to get the brackets closer to each other.
One can use the e-TeX \middle command as follows:
\newcommand{\multibinom}[2]{
\left(\!\middle(\genfrac{}{}{0pt}{}{#1}{#2}\middle)\!\right)
}
This assumes that you are using the AMSmath package. If not, replace \genfrac with the appropriate construct using \atop.
(Of course this is a hack: the proper solution would be scalable glyphs for the doubled parenthesis, but I can't find any fonts that provide it.)
I'm surprised it wasn't googlable either, so I'll provide a solution here for posterity's sake.
It is also possible to define two different new commands, using \tbinom and \dbinom (section 4.11.2 of the User's Guide for the amsmath Package):
\documentclass{article}
\usepackage{amsmath}
\newcommand{\inlinebnm}[2]{\ensuremath{\big(\!\tbinom{#1}{#2}\!\big)}}
\newcommand{\displybnm}[2]{\bigg(\!\!\dbinom{#1}{#2}\!\!\bigg)}
\begin{document}
Text $\inlinebnm{a}{b}$ text. %% inline
Text \inlinebnm{a}{b} text. %% inline (also ok thanks to ensuremath)
\[
\displybnm{a}{b} %% display-style
\]
\end{document}

Alternative input to LaTex newcommand

Is there an alternative way to enter multiple arguments to LaTex newcommand constructions? I have defined
\newcommand{\nuc}[2]{\ensuremath{^{\text{#1}}\text{#2}}}
and I would like to call the command through
\nuc{12,C}
and not
\nuc{12}{C}.
I have created other commands with even more arguments and my poor pinky can't handle all the brackets.
Thanks!
Maybe you will like it.
\def\nuc #1,#2.{\ensuremath{^{\text{#1}}\text{#2}}}
Sample of using:
\nuc 12,C.
Note. Use the dot at the end.
I like to praise perltex for defining complicated functions. This isn't complicated, but you can extend it quite impressively.
%myfile.tex
\documentclass{article}
\usepackage{perltex}
\perlnewcommand{\commafrac}[1]{
$input = shift;
#inputs = split(/,/, $input);
return "\\ensuremath{\\frac{$inputs[0]}{$inputs[1]}}";
}
\begin{document}
One half is $\commafrac{1,2}$.
\end{document}
Compile with perltex --latex=pdflatex myfile.tex. I know that \frac wasn't your example, but I find it a visually appealing one.
Use plain TeX \def:
\makeatletter
\newcommand*{\nuc}[1]{\nuc##1\#nil}
\newcommand*{\nuc#}{}
\protected\def\nuc##1,#2\#nil{\ensuremath{^{\text{#1}}\text{#2}}}
\makeatother
As fas as I know \nuc{12}{c} is the only way. If you don't want to put all the "}{"s, let the editor do it. Write \nuc{12,c} first, then replace all commas with "}{"s.

How to write programming code containing the ' character in latex?

I am trying to write programming code in latex using the verbatim environment, but when I write
\begin{verbatim}
char ch = 'x';
\end{verbatim}
then the ' -characters around x are displayed incorrectly (they look "curly"). How can I fix this problem?
Load the upquote package to fix this issue in verbatim mode.
If you want straight quotes in monospaced text mode (e.g., \texttt{...}), or indeed in any other font, then you can use the \textquotesingle command defined in the textcomp package:
\documentclass{article}
\usepackage{upquote,textcomp}
\begin{document}
\newcommand\upquote[1]{\textquotesingle#1\textquotesingle}
\verb|'c'| \texttt{\upquote{h}}
\textsf{\upquote{h}} \upquote{h}
\end{document}
This will work well for fonts in any encoding rather than depending on a specific glyph slot (such as \char13 in the default OT1 encoding).
Adding \usepackage{upquote} to my preamble was sufficient.
Perhaps older versions of LaTeX or upquote required more work.
I have
What is wrong?
New
If you want to get something like this
write
\makeatletter
\let \#sverbatim \#verbatim
\def \#verbatim {\#sverbatim \verbatimplus}
{\catcode`'=13 \gdef \verbatimplus{\catcode`'=13 \chardef '=13 }}
\makeatother
For displaying source code, you might consider using the listings package; it is quite powerful and offers an option to display “straight” quotation marks.
If you're seeing curly single right quotes in a verbatim environment, then the single right quote in your typewriter font is curly, and that's the correct one to use for what you're doing (which I assume is displaying some C code).
\textsf{``} and \textsf{''} come pretty close to straight quotes. No need for using any special packages.
This is what I got from another source, and this works.
Use `` to start the double quotes (this symbol is below ~ symbol on our keyboard)
Use '' to close the double quotes (this symbol is below the " symbol on our keyboard)
So, `` quote double, unquote double''
Same goes for single quotes, `quote single, unquote single'

Resources