6

Is there a simpler way to write k = 1, 2, ..., 25, instead of $k$ = $1$, $2$, \dots, $25$?

user87407
  • 163

2 Answers2

5

Disregarding any style conventions here's a short wrapper` macro -- judge whether this is 'easier':

\documentclass{article}

\usepackage{etoolbox}
\newcommand{\nrange}[4][]{%
  #2=%
  \ifblank{#1}{% Optional argument empty? Yes? Then omit the second number in the enumeration
    #3, \dots ,#4%
  }{%
    #3, #1, \dots,#4%
  }%
}

\begin{document}
$\nrange{k}{1}{25}$

$\nrange[2]{k}{1}{25}$

\end{document}

enter image description here

Without etoolbox package:

\documentclass{article}

\makeatletter
\newcommand{\nrange}[4][]{%
  \def\@temp@a{#1}%
  #2=%
  \ifx\@temp@a\@empty
  #3, \dots ,#4%
  \else
  #3, #1, \dots,#4%
  \fi
}
\makeatother

\begin{document}
$\nrange{k}{1}{25}$

$\nrange[]{k}{1}{25}$

$\nrange[2]{k}{1}{25}$

\end{document}
  • Please note that there is \numlist from the siunitx package, but this addresses a different purpose, in my point of view (and should be used in textmode only) –  Jul 21 '16 at 05:50
4

Never say never, but I can't think of an instance where you should ever have mathematical operators outside of math mode, including =, when you're writing an equation, or otherwise typesetting maths. This seems to be a common mistake that people make.

Put the entire formula, equation, expression, mathematical object in math mode.

$k = 1, 2, \dots, 25$

enter image description here

$...$ doesn't primarily mean mathify this symbol. It never means italicise. It usually isn't a way to get symbols to display that you can't display otherwise. $...$ and \[...\] put you in math mode (inline math and display math respectively), a mode set up for writing mathematics. All of the mathematical expression should go in there. It gives you appropriate spacing, appropriate formatting, is relied upon by certain symbols, that sort of thing. Also, LaTeX is about logical structure. If you have a single mathematical object, it should be treated as one and everything should be placed between the $...$.

Many bad things will happen if you just enclose each individual entity in $...$ on the basis of whether it works without $...$ or not, see:

Example 1

Example 2

However, there is some exception to this.

If you are writing a sentence like:

We select three variables, a, b, and c.

You may choose to write this:

We select three variables, $a$, $b$, and $c$.

The reason you might do this is you might choose to think of a, b and c as individual mathematical objects within a larger sentence, to which the punctuation and the word and belong.

Commas within math mode may look different to those outside of it, especially if you are using a different font for the rest of the document to the one you use in mathematics, or even just a different style of font. For example:

enter image description here

\textsf{We select three variables, $a$, $b$, and $c$. \quad We select
  three variables, $a, b, \text{ and } c.$}
Au101
  • 10,278