Graphics, Figures & TablesUser-defined environment

Information and discussion about graphics, figures & tables in LaTeX documents.
Post Reply
ge0rge04
Posts: 10
Joined: Sun Mar 28, 2010 12:23 am

User-defined environment

Post by ge0rge04 »

Hi all.

I tried to create a new environment for this matrix(array)

Code: Select all

\[
  F(x)=\left(\negthickspace
  \begin{array}{cc}
  \\[-6pt]
  \dfrac{x}{2} & 0 \\[6pt]
  0 & x^2\\[2pt]
  \end{array}\negthickspace\right)
\]
the code that initializes the matrix with some values

Code: Select all


\begin{document}
\begin{matrice}
\item 1st
\item 2nd
\item 3rd
\item 4th
\end{matrice}
\end{document}
I'm having a little trouble writing the code for the environment.
From a working example i figured out that it should be something like

Code: Select all

\newenvironment{matrice}{
% the first codebox copy-pasted here
}
and for the second argument i just tried to copy-paste the first code.
pdflatex reports errors at the \item lines.

How do i relate the 1st,2nd...variables to the environment defined ?
Thanks.

Recommended reading 2024:

LaTeXguide.org • LaTeX-Cookbook.net • TikZ.org

NEW: TikZ book now 40% off at Amazon.com for a short time.

And: Currently, Packt sells ebooks for $4.99 each if you buy 5 of their over 1000 ebooks. If you choose only a single one, $9.99. How about combining 3 LaTeX books with Python, gnuplot, mathplotlib, Matlab, ChatGPT or other AI books? Epub and PDF. Bundle (3 books, add more for higher discount): https://packt.link/MDH5p

gmedina
Posts: 2313
Joined: Wed Jul 11, 2007 11:45 pm

User-defined environment

Post by gmedina »

Hi,

dependending on your intent you could also use a command with four arguments. Something along these lines:

Code: Select all

\documentclass{article}
\usepackage{amsmath}

\newcommand\mymatrix[4]{
  \ensuremath{%
  F(x)=\left(\negthickspace
  \begin{array}{cc}
  \\[-6pt]
  #1 & #2 \\[6pt]
  #3 & #4\\[2pt]
  \end{array}\negthickspace\right)
  }
}

\begin{document}

\mymatrix{\dfrac{x}{2}}{0}{0}{x\sp{2}}

\end{document}
1,1,2,3,5,8,13,21,34,55,89,144,233,...
ge0rge04
Posts: 10
Joined: Sun Mar 28, 2010 12:23 am

Re: User-defined environment

Post by ge0rge04 »

hi again
your example was fine, but my task clearly specifies to use \newenvironment only
what should i change in this case?
ge0rge04
Posts: 10
Joined: Sun Mar 28, 2010 12:23 am

User-defined environment

Post by ge0rge04 »

i solved it
i'll paste the code, maybe someone will find it usefull

Code: Select all

\documentclass{report}
\usepackage{amsmath}
\newenvironment{matrice}[4]{
  
  $$
  F(x)=\left(\negthickspace
  \begin{array}{cc}
  \\[-6pt]
  #1 & #2 \\[6pt]
  #3 & #4\\[2pt]
  \end{array}\negthickspace\right)
  $$
}

\begin{document}

%\begin{matrice}{\displaystyle \frac{x}{2}}{0}{0}{x\sp{2}}
\begin{matrice}{\dfrac{x}{2}}{0}{0}{x\sp{2}}
\end{matrice}
\end{document}
User avatar
localghost
Site Moderator
Posts: 9202
Joined: Fri Feb 02, 2007 12:06 pm

User-defined environment

Post by localghost »

Introducing a displayed math environment with $$…$$ is no good idea (see l2tabu).
Post Reply