# Writing a nice function

How to write a function mapping like this: MWE

\documentclass{standalone}
\usepackage{amsmath,amssymb}
\begin{document}
$\begin{matrix} f'\colon & A \to \mathbb{R} \\ & x\mapsto f(x)=x^2 \end{matrix}$
\end{document}


It is not alignment well.

and it must be like

• Please complete the code rather than posting mere fragments, which are of much less help. How should it look? Don't use  in LaTeX.
– cfr
Jan 27, 2016 at 3:35
• I believe tex.stackexchange.com/a/32024/21344 has the answer you seek. :-) Jan 27, 2016 at 3:55

(The following code uses math examples you posted initially.)

Depending on the type of alignment you want, one of the following two solutions may work for you.

\documentclass{article}
\usepackage{array}
\begin{document}

If you want flush-left alignment:
$f\colon \begin{array}{>{\displaystyle}l} X \rightarrow Y \\ x\mapsto f(x)=\frac{x-1}{2} \end{array}$

\bigskip
If the arrows have to be aligned vertically:
$f\colon \begin{array}{>{\displaystyle}r @{} >{{}}c<{{}} @{} >{\displaystyle}l} X &\rightarrow& Y \\ x &\mapsto& f(x)=\frac{x-1}{2} \end{array}$
\end{document}


Addendum: To align the first row of the array with f\colon, provide the [t] placement option after \begin{array}

\documentclass{article}
\usepackage{array}
\begin{document}
If \verb+f\colon+ should be on the same line as \verb+X\to Y+:
$\setlength\arraycolsep{0pt} f\colon \begin{array}[t]{ >{\displaystyle}r >{{}}c<{{}} >{\displaystyle}l } X &\to& Y \\ x &\mapsto& f(x)=\frac{x-1}{2} \end{array}$
\end{document}

• can it be improved for f and X \to Y in the same line?
– L F
Jan 27, 2016 at 3:58
• @LuisFelipe - Sure: Just insert [t] after \begin{array}, i.e., write \begin{array}[t]{...}.
– Mico
Jan 27, 2016 at 4:00