7

I cannot compile the following files. I get multiple errors that I can't explain. Can anyone help me?

 \documentclass{article}
 \usepackage{graphicx}

 \begin{document}
 \title{Project 1}
 \author{[email protected]}

 \section{Tools}
 Text
 \section{Algorithm}

 Text. \newline
 Text:

 \[ min_{\textbf{w}} \sum_{i=1}^n (y_i - \textbf{w}^T \textbf{x_i})^2 + \lambda \|\textbf{w}\|_2^2 \]

 \newline
 Text:

 Text

\section{Features}
\section{Parameters}
\end{document}
2
  • 1
    I don't see why @ should be a problem. I have never escaped it. (It's catcode is other anyways, it cannot cause any problems and it surely doesn't belong in mathmode).
    – yo'
    Nov 1, 2013 at 14:33
  • 1
    The problem here is this: When you use \textbf inside math-mode, that's okay. But that turns its argument into text-mode, yet you have x_i which requires math-mode to set the subscript. For this it seems like you could be after something like \mathbf{x}_i instead.
    – Werner
    Nov 1, 2013 at 14:47

3 Answers 3

4

You want the variable denoting a vector to be boldface, not its subscript. For making bold a letter in math mode the best way is to use \mathbf; however it's even better to hide this in a personal command:

\newcommand{\vect}[1]{\mathbf{#1}}

so that your document becomes

\documentclass{article}
\usepackage{amsmath} % necessary for math intensive documents

\newcommand{\vect}[1]{\mathbf{#1}}

\begin{document}

\title{Project 1}
\author{[email protected]}

\maketitle

\section{Tools}

Text.

\section{Algorithm}

Text. Text:
\[
\min_{\vect{w}} \sum_{i=1}^n (y_i - \vect{w}^T \vect{x}_i)^2 + 
  \lambda \lVert\vect{w}\rVert_2^2
\]
Text.

\end{document}

Why doing it indirectly? There are several reasons to. Let's see the two main ones:

  1. You're marking up your document; when you want to find vectors in the typescript, you'll look for \vect rather than \mathbf that might have been used also for other purposes.

  2. If you decide that, after all, boldface italic is better (the ISO regulations prescribe this for vectors, for instance), you can simply change one line in your document (and add the call to a package):

    \usepackage{bm}
    \newcommand{\vect}[1]{\bm{#1}}
    

Notes on the typing style.

  1. Don't use \newline; paragraphs should be simply separated by a blank line in the typescript.

  2. Never leave blank lines before a displayed equation in the typescript.

  3. The most common operators have already a definition: use \min for the “minimum” operator.

  4. Note that it's \vect{x}_i and not \vect{x_i}, because the subscript is appended to the variable name and it shouldn't be specially formatted.

  5. With amsmath (that's recommended for documents using mathematics), instead of \| for representing a double bar, it's better to use the specific delimiters \lVert and \rVert. Look at the documentation of mathtools for even better ways to mark up a document (a \norm command is explained).

3
  • Why didn't you use \lvert...\rvert? Nov 1, 2013 at 17:44
  • 1
    @Marienplatz I'm asking it myself. ;-)
    – egreg
    Nov 1, 2013 at 19:02
  • I see. What is the difference between \lVert...\rVert and \lvert...\rvert? Nov 1, 2013 at 19:10
3

Use \mathbf in formulas, \textbf is not allowed there.

 \documentclass{article}
    \usepackage{graphicx}

    \begin{document}
    \title{Project 1}
    \author{[email protected]}
    \section{Tools}
    Text

    \section{Algorithm}

    Text. 

    Text: %if you don't want the next line to be indented, use \noindent at the beginning of the line.

    \[ \min_{\mathbf{w}}\sum_{i=1}^n (y_i - \mathbf{w}^T \mathbf{x}_i)^2 + \lambda \|\mathbf{w}\|_2^2 \]


    Text:

    Text
    \section{Features}

    \section{Parameters}

    \end{document}
9
  • What is the reason why you change @ to $@$?
    – yo'
    Nov 1, 2013 at 14:34
  • I learned to not use the @ outside of macros. Is this wrong?
    – Chris
    Nov 1, 2013 at 14:35
  • 1
    Well, @ is of catcode other, so you can use it as much as you want. You only have to be carefull about macro names containing @, but that has nothing to do with an e-mail address.
    – yo'
    Nov 1, 2013 at 14:37
  • Ah, ok, thanks. I will change the code above.
    – Chris
    Nov 1, 2013 at 14:38
  • 1
    Yeah, and please, change as well \mathbf{x_i} to \mathbf{x}_i, since the first one obviously doesn't make sense ;)
    – yo'
    Nov 1, 2013 at 14:40
2
\documentclass[preview,border=12pt,12pt]{standalone}% change this line with your own document class below
%\documentclass{article}
%\usepackage{graphicx} <=== do you really need this graphicx package when you don't import any images?

% I move this setting to the preamble
\title{The Joy of \TeX}
\author{[email protected]}
\date{\today}

% you don't need the following two lines in your production
\newlength\buffer
\buffer=\parindent\relax

\usepackage{lipsum}% disable this package in your production. I use here just to produce dummy text.
\usepackage{amsmath}% use this package if you want to get more andvanced mathematics macros.

\begin{document}
\parindent=\buffer\relax% you don't need this line in your production

\maketitle


\section{Tools}
\lipsum[1] 

\section{Algorithm}
\lipsum[2]

The following equation is fun, (by the way, you don't need to give a newline before a displayed equation)
\[ 
    \min_{\mathbf{w}} 
        \sum_{i=1}^n \left(y_i - \mathbf{w}^T \mathbf{x}_i\right)^2 
        + \lambda \lvert\mathbf{w}\rvert_2^2 
\]
Isn't it? You don't need to add a newline after the displayed equation.

\lipsum[3]
\section{Features}
\section{Parameters}
\end{document}

enter image description here

0

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .