> For the complete documentation index, see [llms.txt](https://overleaf-pro.ayaka.space/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://overleaf-pro.ayaka.space/latex/es/preguntas-y-respuestas/16-how-can-i-include-matlab-code-in-my-latex-document.md).

# ¿Cómo puedo incluir código MATLAB en mi documento LaTeX?

El [`matlab-prettifier` paquete](https://www.ctan.org/pkg/matlab-prettifier) puede utilizarse para componer código MATLAB y fue escrito como respuesta a una [pregunta en tex.stackexchange](https://tex.stackexchange.com/questions/75116/what-can-i-use-to-typeset-matlab-code-in-my-document).

### El entorno lstlisting

Después de incluir el `matlab-prettifier` paquete en su documento mediante

```latex
\usepackage{matlab-prettifier}
```

puede usar el `lstlisting` entorno para componer código MATLAB:

```latex
\begin{lstlisting}[<options>]
código MATLAB...
\end{lstlisting}
```

donde `<options>` es una lista de parámetros separada por comas cuyos valores se establecen mediante pares clave–valor (es decir, `parameter=value`).

* **Nota**: El `matlab-prettifier` paquete se basa en el `listings` paquete, lo que permite usar características proporcionadas por `listings`, incluidos parámetros dentro de `<options>` del `lstlisting` entorno. Sin embargo, algunos `listings` parámetros deberían *no* utilizarse, y se aconseja a los lectores consultar el [`matlab-prettifier` documentación](http://mirrors.ctan.org/macros/latex/contrib/matlab-prettifier/matlab-prettifier.pdf) para más detalles.

La apariencia visual del código MATLAB compuesto puede cambiarse usando el `style` parámetro, que admite tres valores:

* `Matlab-editor`
* `Matlab-bw`
* `Matlab-Pyglike`

Los siguientes ejemplos muestran cómo usar esos estilos.

### Ejemplos

Aquí hay un ejemplo, usando código MATLAB de `matlab-prettifier`la documentación de ’, que aplica el `Matlab-editor` estilo:

```latex
\documentclass[a4paper]{article}
\usepackage{matlab-prettifier}
\begin{document}

\begin{lstlisting}[style=Matlab-editor]
%% Código de ejemplo de Matlab
!mv test.txt test2.txt
A = [1, 2, 3;... foo
     4, 5, 6];
s = 'abcd';
for k = 1:4
  Disp(s(k)) % bar
end
%{
crear un vector fila x y luego invertirlo
%}
x = linspace(0,1,101);
y = x(end:-1:1);
\end{lstlisting}
\end{document}
```

[Abre este ejemplo en Overleaf](https://www.overleaf.com/docs?engine=pdflatex\&snip_name=A+matlab-prettifier+example\&snip=%5Cdocumentclass%5Ba4paper%5D%7Barticle%7D%0A%5Cusepackage%7Bmatlab-prettifier%7D%0A%5Cbegin%7Bdocument%7D%0A%0A%5Cbegin%7Blstlisting%7D%5Bstyle%3DMatlab-editor%5D%0A%25%25+Sample+Matlab+code%0A%21mv+test.txt+test2.txt%0AA+%3D+%5B1%2C+2%2C+3%3B...+foo%0A+++++4%2C+5%2C+6%5D%3B%0As+%3D+%27abcd%27%3B%0Afor+k+%3D+1%3A4%0A++Disp%28s%28k%29%29+%25+bar%0Aend%0A%25%7B%0Acreate+row+vector+x%2C+then+reverse+it%0A%25%7D%0Ax+%3D+linspace%280%2C1%2C101%29%3B%0Ay+%3D+x%28end%3A-1%3A1%29%3B%0A%5Cend%7Blstlisting%7D%0A%5Cend%7Bdocument%7D)

Este ejemplo produce la siguiente salida:

![Código MATLAB compuesto en LaTeX en Overleaf](/files/63c1b55fcc64a9cba1587a69cb8bd268a206fd71)

El siguiente ejemplo establece valores para los `frame` y `numbers` parámetros proporcionados por el `listings` paquete:

```latex
\documentclass[a4paper]{article}
\usepackage{matlab-prettifier}
\begin{document}

\begin{lstlisting}[
frame=single,
numbers=left,
style=Matlab-Pyglike]
%% Código de ejemplo de Matlab
!mv test.txt test2.txt
A = [1, 2, 3;... foo
     4, 5, 6];
s = 'abcd';
for k = 1:4
  Disp(s(k)) % bar
end
%{
crear un vector fila x y luego invertirlo
%}
x = linspace(0,1,101);
y = x(end:-1:1);
\end{lstlisting}
\end{document}
```

[Abre este ejemplo en Overleaf](https://www.overleaf.com/docs?engine=pdflatex\&snip_name=A+second+matlab-prettifier+example\&snip=%5Cdocumentclass%5Ba4paper%5D%7Barticle%7D%0A%5Cusepackage%7Bmatlab-prettifier%7D%0A%5Cbegin%7Bdocument%7D%0A%0A%5Cbegin%7Blstlisting%7D%5B%0Aframe%3Dsingle%2C%0Anumbers%3Dleft%2C%0Astyle%3DMatlab-Pyglike%5D%0A%25%25+Sample+Matlab+code%0A%21mv+test.txt+test2.txt%0AA+%3D+%5B1%2C+2%2C+3%3B...+foo%0A+++++4%2C+5%2C+6%5D%3B%0As+%3D+%27abcd%27%3B%0Afor+k+%3D+1%3A4%0A++Disp%28s%28k%29%29+%25+bar%0Aend%0A%25%7B%0Acreate+row+vector+x%2C+then+reverse+it%0A%25%7D%0Ax+%3D+linspace%280%2C1%2C101%29%3B%0Ay+%3D+x%28end%3A-1%3A1%29%3B%0A%5Cend%7Blstlisting%7D%0A%5Cend%7Bdocument%7D)

Este ejemplo produce la siguiente salida:

![Código MATLAB compuesto en LaTeX en Overleaf](/files/c0ed09956eeef9063a549a2eab5b6f06c69aac6e)

### Uso de código MATLAB almacenado en un archivo

En lugar de incluir código MATLAB directamente en su documento LaTeX, use el `\lstinputlisting` comando para importar y componer código contenido en un archivo externo:

```latex
\lstinputlisting[<options>]{<filename>}
```

donde

* `<options>` es una lista de parámetros separada por comas proporcionada como pares clave–valor [(véase arriba)](#matopts);
* `<filename>` es la ruta a un archivo que contiene código MATLAB.

Por ejemplo, para importar el archivo `sample.m` y componerlo en el `Matlab-bw` estilo, escribiría

```latex
\lstinputlisting[style=Matlab-bw]{sample.m}
```

El siguiente ejemplo coloca un marco alrededor del código, numera las líneas de código (a la izquierda) y usa el `Matlab-bw` estilo:

```latex
\lstinputlisting[
frame=single,
numbers=left,
style=Matlab-editor
]{sample.m}
```

Seleccione el `Abre este ejemplo en Overleaf` enlace ([véase abajo](#openol)) para crear automáticamente un proyecto de Overleaf que incluye el `sample.m` archivo de código MATLAB.

**`main.tex`**

```latex
\documentclass[a4paper]{article}
\usepackage{matlab-prettifier}
\begin{document}
\title{Importar y componer código MATLAB contenido en un archivo}
\lstinputlisting[
frame=single,
numbers=left,
style=Matlab-bw
]{sample.m}

\end{document}
```

**`sample.m`**

```latex
%% Código de ejemplo de Matlab
!mv test.txt test2.txt
A = [1, 2, 3;... foo
     4, 5, 6];
s = 'abcd';
for k = 1:4
  Disp(s(k)) % bar
end
%{
crear un vector fila x y luego invertirlo
%}
x = linspace(0,1,101);
y = x(end:-1:1);
```

[Abra este ejemplo en Overleaf (crea el `sample.m` archivo).](https://www.overleaf.com/docs?engine=pdflatex\&snip_name\[]=main.tex\&snip\[]=%5Cdocumentclass%5Ba4paper%5D%7Barticle%7D%0A%5Cusepackage%7Bmatlab-prettifier%7D%0A%5Cbegin%7Bdocument%7D%0A%5Ctitle%7BImporting+and+typesetting+MATLAB+code+contained+in+a+file%7D%0A%5Clstinputlisting%5B%0Aframe%3Dsingle%2C%0Anumbers%3Dleft%2C%0Astyle%3DMatlab-bw%0A%5D%7Bsample.m%7D%0A%0A%5Cend%7Bdocument%7D\&snip_name\[]=sample.m\&snip\[]=%25%25+Sample+Matlab+code%0A%21mv+test.txt+test2.txt%0AA+%3D+%5B1%2C+2%2C+3%3B...+foo%0A+++++4%2C+5%2C+6%5D%3B%0As+%3D+%27abcd%27%3B%0Afor+k+%3D+1%3A4%0A++Disp%28s%28k%29%29+%25+bar%0Aend%0A%25%7B%0Acreate+row+vector+x%2C+then+reverse+it%0A%25%7D%0Ax+%3D+linspace%280%2C1%2C101%29%3B%0Ay+%3D+x%28end%3A-1%3A1%29%3B\&main_document=main.tex)

Este ejemplo produce la siguiente salida:

![Código MATLAB compuesto en LaTeX en Overleaf](/files/eb360b22ce1c9e8b0d9da75e145f2054956e387b)


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://overleaf-pro.ayaka.space/latex/es/preguntas-y-respuestas/16-how-can-i-include-matlab-code-in-my-latex-document.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
