Punctuation in LaTeX formulas - latex

It seems to be common practice, when writing mathematics, to add punctuation to displayed formulas.
Is there any trick to avoid putting the punctuation mark inside the formula?
I want to avoid
Consider the function
\[ \sin(x).\]
I'd rather have something like:
Consider the function
\[ \sin(x)\].
But of course the full stop is displayed below the formula.
Is there a clever way to separate formulas and punctuation in LaTeX?

\catcode`\#=11
\let\seveendformula\]
\def\]{\#ifnextchar.\PointAndEndFormula\seveendformula}
\def \PointAndEndFormula #1{.\seveendformula}
\catcode`\#=12
Add
More complex solution works with .,?!;: :
\catcode`\#=11
\def\addtopunct#1{\expandafter\let\csname punct#\meaning#1\endcsname\let}
\addtopunct{.} \addtopunct{,} \addtopunct{?}
\addtopunct{!} \addtopunct{;} \addtopunct{:}
\let\seveendformula\]
\def\PunctAndEndFormula #1{#1\seveendformula}
\def\]{\futurelet\punctlet\checkpunct#i}
\def\checkpunct#i{\expandafter\ifx\csname punct#\meaning\punctlet\endcsname\let
\expandafter\PunctAndEndFormula
\else \expandafter\seveendformula\fi}
\catcode`\#=12

There's also the issue of which font the punctuation should be in. You won't see a problem until you try a different math font such as Euler. Then commas and periods are clearly different in text mode and in math mode. I've written text-mode punctuation in displayed formulas as \mbox{,} or lazily as \mbox, just before $$.

You can load the breqn package (compatible with amsmath) and have this behaviour inbuilt with its dmath environment:
Consider the function
\begin{dmath}\sin(x)\end{dmath}.
Will output the same as \[\sin(x).\], i.e., as if the dot was inside the expression.
As a bonus, you'll also get automatic line-breaking in your math equation.

Putting the punctuation inside a display environment is the usual way. The problem is that when Latex processes the \], it ends the mathbox, so anything following will be part of a new vertical box.
You could try something like:
\hbox{\[My formula\]}.
This is not tested, and probably has spacing issues, but if you are interested in this kind of solution, I could try and get something working.
FWIW, you might be interested in https://mathoverflow.net/questions/6675/periods-and-commas-in-mathematical-writing

Related

how to add subscript(or superscript) in notion block?

i am new in using notion as a notebook.Before this,i use oneNote instead,because i found that notion is more convinient in same ways.But one thing making me annoyed is that i can't add subscript(or superscript) in notion text block。Are there any fellows can handle this?
as shown:
text with subscript i need
when i edit in notion block i get like this
For subscript:
/math
Then C_1 renders C1
For superscript:
/math
Then C^2 renders C2
In the latest version as of writing, you could use:
$$c^2$$ - for superscript
$$c_2$$ - for subscript
If you write any mathematical equation between $$ $$, it will render beautifully on notion.
For more than 1 character superscript
/math
P^{xyz}
Result: Pxyz
For more than 1 character subscript
/math
P_{xyz}
Result: Pxyz
In fact, this works for 1 or many characters as subscript or superscript.
Even I just started using notion and faced the same problem. The solution I found was to keep a tab of laTex containing the syntax for all the required math formulae open. It is only temporary as you will get used to them pretty quickly.
You can use tex to write things is superscript or subscript or basically any type of formulae , but its still in tex.
I did and worked on my Notion
Step 1: Choose what to add subscript(or superscript) in notion block (Example a^n)
Step 2: Choose symbol sqrt(x)
Step 3: Press "Done"
In Notion, there are two ways to add math formulas: inline equations and block equations. Both use KaTeX formatting to show the formulas. KaTeX is a TeX display formulas method based on JavaScrpt language. When you place a block or inline equation, for your example, you have to do something like that in the code:
w_{1}=0 ~,~ w_{2}=1
Note that spaces are not rendered, you gotta use ~ separate things, and the { } on these subscripts are optional because when not placed, the "" will take just the first next character. To do superscript, change the "" to "^". You can combine both in the same main variable, like w_2^2=1.
In the following links, you can see a Notion's guide about how to use it, the supported features KaTeX allows you to do, and a link to easy creating formulas.
Notion:
https://www.notion.so/help/math-equations
KaTeX Supported Functions:
https://katex.org/docs/supported.html or
https://katex.org/docs/support_table.html
Easy web TeX editor:
https://atomurl.net/math/
Take time to try and explore writing by typping. Then, when you get used to it, it flows easily like writing regular text.
For anyone stumbling upon this question who does not want to write math equations but simply wants to add a couple of superscript or exponent elements to a Notion page, for instance to write footnotes: there seems to be no way to add "simple" superscript (inline and with the same font), but you can use superscript unicode characters, for instance with a tool such as this one.
It is not perfect as many characters are not available, but will cover many¹ common² uses³ of superscriptᶜᶦᵗᵃᵗᶦᵒⁿ ⁿᵉᵉᵈᵉᵈ.

How to start math formulae in new line if bad boxed in latex?

What sort of code should I put in preamble, such that every math fomula (in dolar signs) that is too long to fit in line width (a.k.a. produces Bad Box) start in new line (like putting // before equation) ? Or is it impossible to detect and prevent Bad Box in compiling ?
You could use \sloppy or \sloppypar, but that will reduce the quality of the line breaks.
Take a look here.
Are you talking about inline math or TeX-style $$? In the first case: think about putting it in its own environment, long inline math isn't a good idea. In the second case, start using align (or the appropriate environment) and place those breaks yourself. Or use breqn, it will do it automatically.
In case it's inline and you want it to stay inline: consider rewriting your sentences, as usual with bad boxes. And consider using microtype if you're building to pdf, it will enhance the flexibility of the fonts, hence, more possibilities for line breaking.
I don't have an actual solution for line breaks in inline math, I usually resort to these tricks. Maybe someone with a better insignt in inline math can help you with that :).

Automatically capitalize first letter of first word in a new sentence in LaTeX

I know one of LaTeX's bragging points is that it doesn't have this Microsoftish behavior. Nevertheless, it's sometimes useful.
LaTeX already adds an extra space after you type a (non-backslashed) period, so it should be possible to make it automatically capitalize the following letter as well.
Is there an obvious way to write a macro that does this, or is there a LaTeX package that does it already?
The following code solves the problem.
\let\period.
\catcode`\.\active
\def\uppercasesingleletter#1{\uppercase{#1}}
\def.{\period\afterassignment\periodx\let\next= }
\def \periodx{\ifcat\space\next \next\expandafter\uppercasesingleletter \else\expandafter\next\fi}
First. second.third. relax.relax. up
\let\period. save period
\catcode\.\active make all periods to be active symbol (like macro).
\def\uppercasesingleletter#1{\uppercase{#1}} defines macro \uppercasesingleletter to make automatically capitalize the following letter.
\def.{\period\afterassignment\periodx\let\next= } writes saved period and checkes the next symbol.
\def \periodx{\ifcat\space\next \next\expandafter\uppercasesingleletter \else\expandafter\next\fi} If the next letter is a space then \uppercasesingleletter is inserted.
ages ago there was discussion of this idea on comp.text.tex, and the general conclusion was you can't do it satisfactorily. satisfactory, in my book, involves not making characters active, but i can't see how that could work at all.
personally, i would want to make space active, and have it then look at \spacefactor and \MakeUppercase the following character if the factor is 3000.
something like
\catcode\ \active % latex already has a saved space character -- \space
\def {\ifhmode% \spacefactor is invalid
% (or something) in vertical mode
\ifnum\spacefactor<3000\else% note: with space active,
% even cs-ended lines need %-termination
\expandafter\gobbleandupper\fi}%
\def\gobbleandupper#1{\def\tempa{#1}\def\tempb{ }%
\ifx\tempa\tempb% can''t indent the code, either :-(
% here, we have another space
\expandafter\gobbleandupper% try again
\else\space% insert a "real" space to soak up the
% space factor
\expandafter\MakeUppercase\fi}%
this doesn't really do the job -- there are enough loose ends to knit a fairisle jumper. for example, given that we can't rely on \everypar in latex, how do you uppercase the first letter of a paragraph?
no ... however much it hurts (which is why i avoid unnecessary key operations) we need to type latex "properly" :-(
I decided to solve it in the following way:
Since I always compile the LaTeX code three times before i okular the result (to get pagination and references right), I decided to build the capitalization of sentences into that process.
Thus, I now have a shell script that calls my capitalization script (written in CRM114) first, then pdflatex three times, and then okular. This way, all the stuff happens as the result of a single command.

Latex - Apply an operation to every character in a string

I am using LaTeX and I have a problem concerning string manipulation.
I want to have an operation applied to every character of a string, specifically
I want to replace every character "x" with "\discretionary{}{}{}x". I want to do
this because I have a long string (DNA) which I want to be able to separate at
any point without hyphenation.
Thus I would like to have a command called "myDNA" that will do this for me instead of
inserting manually \discretionary{}{}{} after every character.
Is this possible? I have looked around the web and there wasnt much helpful
information on this topic (at least not any I could understand) and I hoped
that you could help.
--edit
To clarify:
What I want to see in the finished document is something like this:
the dna sequence is CTAAAGAAAACAGGACGATTAGATGAGCTTGAGAAAGCCATCACCACTCA
AATACTAAATGTGTTACCATACCAAGCACTTGCTCTGAAATTTGGGGACTGAGTACACCAAATACGATAG
ATCAGTGGGATACAACAGGCCTTTACAGCTTCTCTGAACAAACCAGGTCTCTTGATGGTCGTCTCCAGGT
ATCCCATCGAAAAGGATTGCCACATGTTATATATTGCCGATTATGGCGCTGGCCTGATCTTCACAGTCAT
CATGAACTCAAGGCAATTGAAAACTGCGAATATGCTTTTAATCTTAAAAAGGATGAAGTATGTGTAAACC
CTTACCACTATCAGAGAGTTGAGACACCAGTTTTGCCTCCAGTATTAGTGCCCCGACACACCGAGATCCT
AACAGAACTTCCGCCTCTGGATGACTATACTCACTCCATTCCAGAAAACACTAACTTCCCAGCAGGAATT
just plain linebreaks, without any hyphens. The DNA sequence will be one
long string without any spaces or anything but it can break at any point.
This is why my idea was to inesert a "\discretionary{}{}{}" after every
character, so that it can break at any point without inserting any hyphens.
This takes a string as an argument and calls \discretionary{}{}{} after each character. The input string stops at the first dollar sign, so you should not use that.
\def\hyphenateWholeString #1{\xHyphenate#1$\wholeString}
\def\xHyphenate#1#2\wholeString {\if#1$%
\else\say{#1}\discretionary{}{}{}%
\takeTheRest#2\ofTheString
\fi}
\def\takeTheRest#1\ofTheString\fi
{\fi \xHyphenate#1\wholeString}
\def\say#1{#1}
You’d call it like \hyphenateWholeString{CTAAAGAAAACAGGACG}.
Instead of \discretionary{}{}{} you can also try \hspace{0pt}, if you like that more (and are in a latex environment). In order to align the right margin, I think you’d need to do some more fine tuning (but see below). The effect is of course minimised by using a font of fixed width.
Revision:
\def\hyphenateWholeString #1{\xHyphenate#1$\wholeString\unskip}
\def\xHyphenate#1#2\wholeString {\if#1$%
\else\transform{#1}%
\takeTheRest#2\ofTheString\fi}
\def\takeTheRest#1\ofTheString\fi
{\fi \xHyphenate#1\wholeString}
\def\transform#1{#1\hskip 0pt plus 1pt}
Steve’s suggestion of using \hskip sounds like a very good idea to me, so I made a few corrections. Note that I’ve renamed the \say macro and made it more useful in that it now actually does the transformation. (However, if you remove the \hskip from \transform, you’ll also need to remove the \unskip in the main macro definition.
Edit:
There is also the seqsplit package which seems to be made for printing DNA data or long numbers. They also bring a few options for nicer output, so maybe that is what you’re looking for…
Debilski's post is definitely a solid way to do it, although the \say is not necessary. Here's a shorter way that makes use of some LaTeX internal shortcuts (\#gobble and \#ifnextchar):
\makeatletter
\def\hyphenatestring#1{\xHyphen#te#1$\unskip}
\def\xHyphen#te{\#ifnextchar${\#gobble}{\sw#p{\hskip 0pt plus 1pt\xHyphen#te}}}
\def\sw#p#1#2{#2#1}
\makeatother
Note the use of \hskip 0pt plus 1pt instead of \discretionary - when I tried your example I ended up with a ragged margin because there's no stretchability. The \hskip adds some stretchable glue in between each character (and the \unskip afterwards cancels the extra one we added). Also note the LaTeX style convention that "end user" macros are all lowercase, while internal macros have an # in them somewhere so that users don't accidentally call them.
If you want to figure out how this works, \#gobble just eats whatever's in front of it (in this case the $, since that branch is only run when a $ is the next char). The main point is that \sw#p is only given one argument in the "else" branch, so it swaps that argument with the next char (that isn't a $). We could just as well have written \def\hyphenate#next#1{#1\hskip...\xHyphen#te} and put that with no args in the "else" branch, but (in my opinion) \sw#p is more general (and I'm surprised it's not in standard LaTeX already).
There is a contrib package on CTAN that deals with typesetting DNA sequences. It does a little more than just line-breaking, for example, it also supports colouring. I'm not sure if it is possible to get the output you are after though, and I have no experience in the DNA-sequence-typesetting area, but is one long string the most readable representation?
Assuming your string is the same, in your preamble, use the \newcommand{}{}. Like this:
\newcommand{\myDNA}{blah blah blah}
if that doesn't satisfy your requirements, I suggest:
2. Break the strings down to the smallest portion, then use the \newcommand and then call the new commands in sequence: \myDNA1 \myDNA2.
If that still doesn't work, you might want to look at writing a perl script to satisfy your string replacement needs.

LaTeX: Prevent line break in a span of text

How can I prevent LaTeX from inserting linebreaks in my \texttt{...} or \url{...} text regions? There's no spaces inside I can replace with ~, it's just breaking on symbols.
Update: I don't want to cause line overflows, I'd just rather LaTeX insert linebreaks before these regions rather than inside them.
\mbox is the simplest answer. Regarding the update:
TeX prefers overlong lines to adding too much space between words on a line; I think the idea is that you will notice the lines that extend into the margin (and the black boxes it inserts after such lines), and will have a chance to revise the contents, whereas if there was too much space, you might not notice it.
Use \sloppy or \begin{sloppypar}...\end{sloppypar} to adjust this behavior, at least a little. Another possibility is \raggedright (or \begin{raggedright}...\end{raggedright}).
Surround it with an \mbox{}
Also, if you have two subsequent words in regular text and you want to avoid a line break between them, you can use the ~ character.
For example:
As we can see in Fig.~\ref{BlaBla}, there is nothing interesting to see. A~better place..
This can ensure that you don't have a line starting with a figure number (without the Fig. part) or with an uppercase A.
Use \nolinebreak
\nolinebreak[number]
The \nolinebreak command prevents LaTeX from
breaking the current line at the point of the command. With the
optional argument, number, you can convert the \nolinebreak command
from a demand to a request. The number must be a number from 0 to 4.
The higher the number, the more insistent the request is.
Source: http://www.personal.ceu.hu/tex/breaking.htm#nolinebreak
Define myurl command:
\def\myurl{\hfil\penalty 100 \hfilneg \hbox}
I don't want to cause line overflows,
I'd just rather LaTeX insert linebreaks before
\myurl{\tt http://stackoverflow.com/questions/1012799/}
regions rather than inside them.

Resources