GeneralInput File in new Command

LaTeX specific issues not fitting into one of the other forums of this category.
Post Reply
GabCR
Posts: 2
Joined: Wed Dec 18, 2013 8:59 pm

Input File in new Command

Post by GabCR »

Hi,

I don't know if this is possible but what I would like to do is to pass as an argument the content of an input'ed .tex file.

My example:
I have a tex file named model.tex which only contains:

512

In my main tex file, I've created a newcommand which is the following:

Code: Select all

\newcommand{\pixel}[1]
{
\ifthenelse{\equal{#1}{128}}{128 x 128; 24${\mu}$${}^{2}$ pixel area; 3.1mm${}^{2}$ effective area}{}
\ifthenelse{\equal{#1}{512}}{512 x 512; 16${\mu}$${}^{2}$ pixel area; 8.19mm${}^{2}$ effective area}{}
\ifthenelse{\equal{#1}{1024}}{1024 x 1024; 13${\mu}$${}^{2}$ pixel area; 13.3mm${}^{2}$ effective area}{}
}

\pixel{\input{model.tex}}\\
This doesn't work and gives me back the following error:
Use of an \@@array doesn't match its definition, and other things after that...


When I simply write

Code: Select all

\input{model.tex}
It writes 512 in the document and when I write

Code: Select all

\pixel{128}\\
it gives me the correct response according to my \newcommand.

Both functions work individually as I want them to but when I try to give the result of my input in my \newcommand, it doesn't work. Does anybody have a trick that could allow me to do that.

Thanks community!

Recommended reading 2024:

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

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

cgnieder
Site Moderator
Posts: 2000
Joined: Sat Apr 16, 2011 7:27 pm

Input File in new Command

Post by cgnieder »

Welcome to the LaTeX community!

The command \input is not expandable but it needs to be as argument to \ifthenelse. You can use the catchfile package which provides the means to save the contents of a file in a macro which then can be fed to \pixel.

Here is an example (where I also changed some details of your text to make them typographically more pleasing; as I don't know the unit µ² I used a dummy name for it):

Code: Select all

\documentclass{article}
\usepackage{catchfile}% get the contents of a file
\usepackage{siunitx}% typesseting of numbers and units
\usepackage{upgreek}
\DeclareSIUnit\myunit{$\upmu^2$}

\newcommand*{\pixel}[1]{% hide end of line the would be turned into a space{
  \ifnum#1=128 % leave space here! it tells \ifnum where the number ends
    $128 \times 128$; \SI{24}{\myunit} pixel area;
    \SI{3.1}{\milli\metre\squared} effective area%
  \fi
  \ifnum#1=512
    $512 \times 512$; \SI{16}{\myunit} pixel area;
    \SI{8.19}{\milli\metre\squared} effective area%
  \fi
  \ifnum#1=1024
    $1024 \times 1024$; \SI{13}{\myunit} pixel area;
    \SI{13.3}{\milli\metre\squared} effective area%
  \fi
}

% for keeping everything in one file:
\usepackage{filecontents}
\begin{filecontents*}{testfile.tex}
  1024
\end{filecontents*}

\begin{document}
\pixel{512}

\CatchFileDef{\model}{testfile.tex}{}
\pixel{\model}

\end{document}
catchfile.png
catchfile.png (8.47 KiB) Viewed 3815 times
Regards
site moderator & package author
GabCR
Posts: 2
Joined: Wed Dec 18, 2013 8:59 pm

Re: Input File in new Command

Post by GabCR »

Thank you so much cgnieder!

It works perfectly.
Post Reply