How to repeat the same content in different tables?
up vote
3
down vote
favorite
I have a list of keywords and descriptions for those keywords. I would like to have tables for the different contexts in which these are used, containing only the relevant ones for the case. Some of these keywords repeat on the different contexts, so it would be ideal to write the description only once and then "reference" that description in each of my tables for ease of maintenance.
Example:
section{Runtype 1 description}
begin{table}
begin{center}
begin{tabular}{ l l }
runtype & Keyword to determine runtype. Can be 1 or 2. \
& I don't want to hardcode this description twice...\
param_1 & Keyword to determine the parameter of runtype = 1 \
end{tabular}
end{center}
end{table}
section{Runtype 2 description}
begin{table}
begin{center}
begin{tabular}{ l l }
runtype & Keyword to determine runtype. Can be 1 or 2. \
& I don't want to hardcode this description twice...\
param_2 & Keyword to determine the parameter of runtype = 2 \
end{tabular}
end{center}
end{table}
tables
New contributor
add a comment |
up vote
3
down vote
favorite
I have a list of keywords and descriptions for those keywords. I would like to have tables for the different contexts in which these are used, containing only the relevant ones for the case. Some of these keywords repeat on the different contexts, so it would be ideal to write the description only once and then "reference" that description in each of my tables for ease of maintenance.
Example:
section{Runtype 1 description}
begin{table}
begin{center}
begin{tabular}{ l l }
runtype & Keyword to determine runtype. Can be 1 or 2. \
& I don't want to hardcode this description twice...\
param_1 & Keyword to determine the parameter of runtype = 1 \
end{tabular}
end{center}
end{table}
section{Runtype 2 description}
begin{table}
begin{center}
begin{tabular}{ l l }
runtype & Keyword to determine runtype. Can be 1 or 2. \
& I don't want to hardcode this description twice...\
param_2 & Keyword to determine the parameter of runtype = 2 \
end{tabular}
end{center}
end{table}
tables
New contributor
1
Welcome to TeX.SE!
– Kurt
10 hours ago
@Kurt Thank you!
– Nordico
9 hours ago
add a comment |
up vote
3
down vote
favorite
up vote
3
down vote
favorite
I have a list of keywords and descriptions for those keywords. I would like to have tables for the different contexts in which these are used, containing only the relevant ones for the case. Some of these keywords repeat on the different contexts, so it would be ideal to write the description only once and then "reference" that description in each of my tables for ease of maintenance.
Example:
section{Runtype 1 description}
begin{table}
begin{center}
begin{tabular}{ l l }
runtype & Keyword to determine runtype. Can be 1 or 2. \
& I don't want to hardcode this description twice...\
param_1 & Keyword to determine the parameter of runtype = 1 \
end{tabular}
end{center}
end{table}
section{Runtype 2 description}
begin{table}
begin{center}
begin{tabular}{ l l }
runtype & Keyword to determine runtype. Can be 1 or 2. \
& I don't want to hardcode this description twice...\
param_2 & Keyword to determine the parameter of runtype = 2 \
end{tabular}
end{center}
end{table}
tables
New contributor
I have a list of keywords and descriptions for those keywords. I would like to have tables for the different contexts in which these are used, containing only the relevant ones for the case. Some of these keywords repeat on the different contexts, so it would be ideal to write the description only once and then "reference" that description in each of my tables for ease of maintenance.
Example:
section{Runtype 1 description}
begin{table}
begin{center}
begin{tabular}{ l l }
runtype & Keyword to determine runtype. Can be 1 or 2. \
& I don't want to hardcode this description twice...\
param_1 & Keyword to determine the parameter of runtype = 1 \
end{tabular}
end{center}
end{table}
section{Runtype 2 description}
begin{table}
begin{center}
begin{tabular}{ l l }
runtype & Keyword to determine runtype. Can be 1 or 2. \
& I don't want to hardcode this description twice...\
param_2 & Keyword to determine the parameter of runtype = 2 \
end{tabular}
end{center}
end{table}
tables
tables
New contributor
New contributor
New contributor
asked 10 hours ago
Nordico
1183
1183
New contributor
New contributor
1
Welcome to TeX.SE!
– Kurt
10 hours ago
@Kurt Thank you!
– Nordico
9 hours ago
add a comment |
1
Welcome to TeX.SE!
– Kurt
10 hours ago
@Kurt Thank you!
– Nordico
9 hours ago
1
1
Welcome to TeX.SE!
– Kurt
10 hours ago
Welcome to TeX.SE!
– Kurt
10 hours ago
@Kurt Thank you!
– Nordico
9 hours ago
@Kurt Thank you!
– Nordico
9 hours ago
add a comment |
3 Answers
3
active
oldest
votes
up vote
1
down vote
accepted
documentclass[12pt]{article}
newcommandzzz{%
runtype & Keyword to determine runtype. Can be 1 or 2. \
& I don't want to hardcode this description twice...}
begin{document}
section{Runtype 1 description}
begin{table}[ht]
begin{center}
begin{tabular}{ l l }
zzz\
param_1 & Keyword to determine the parameter of runtype = 1 \
end{tabular}
end{center}
end{table}
section{Runtype 2 description}
begin{table}[ht]
begin{center}
begin{tabular}{ l l }
zzz\
param_2 & Keyword to determine the parameter of runtype = 2 \
end{tabular}
end{center}
end{table}
end{document}
Ok, this works but feels like using a cannon to kill a fly. More precisely: I need to use this with around a hundred keywords, is defining a hundred newcommand to use as a "reference dictionary" advisable? How can I use descriptive names (tablekey_keyname) without using underscores?
– Nordico
9 hours ago
@Nordico As to how to use underscores in a command name, options are limited: 1) change catcode of_
(not recommended, as it breaks subscript math), or 2)csdef
andcsuse
with theetoolbox
package (recommended if you insist on underscore in name)
– Steven B. Segletes
9 hours ago
I mean, not necessarily an underscore, it can be other symbol ("tablekey.keyname") or a way of structuring the commands (like an equivalent to C++ namespaces, so I could do "tablekey::keyname"), anything that makes it more human readable/usable than just altogether "tablekeykeyname".
– Nordico
9 hours ago
1
@Nordico In my example, addcatcode`:=11
as the first line afterdocumentclass
, This changes:
to be a "letter". Thennewcommandzz:z{...}
can be defined and later used aszz:z
. What I don't know is if this breaks anything (e.g., spacing, hyphenation), if you use:
as a normal character.
– Steven B. Segletes
9 hours ago
add a comment |
up vote
2
down vote
Below, I'm defining the following commands:
setkeyword{<keyword>}{<description>}
can be used to declare a keyword.
getkeyword{<keyword>}
retrieves the description you provided.
keywordtable{<key1>,<key2>,…}
displays a table containing the descriptions corresponding to a comma-separated list of keywords.
(Note that keywords containing underscores are allowed.)
documentclass{article}
usepackage[T1]{fontenc} %% <- necessary for _ to be displayed correctly
usepackage{etoolbox} %% <- for csuse, csdef, ifcsdef and forcsvlist
usepackage{tabularx}
newcommand*keyname[1]{keyw@#1} %% <- just prepends key@
%% Define/retrieve a new keyword:
newcommandsetkeyword[2]{csdef{keyname{#1}}{#2}}
newcommand*getkeyword[1]{%
ifcsdef{keyname{#1}}{% %% <- if the key is defined...
csuse{keyname{#1}}% %% <- return the description
}{% %% <- otherwise...
errmessage{Undefined key: #1}% %% <- ERROR
}%
}
%% Display a table describing a list of keywords:
newcommand*keywordtable[1]{%
begin{tabularx}{linewidth}{lX}
forcsvlist{tableentry}{#1} %% <- apply tableentry to each value in #1
end{tabularx}%
}
newcommand*tableentry[1]{%
formattableentry{detokenize{#1}}{getkeyword{#1}}%
}
newcommandformattableentry[2]{ #1 & #2 \ }
% %% Declaration of keywords:
setkeyword{runtype}{Keyword to determine runtype. Can be 1 or 2. par
I don't want to hardcode this description twiceldots}
setkeyword{param_1}{Keyword to determine the parameter of runtype = 1}
setkeyword{param_2}{Keyword to determine the parameter of runtype = 2}
begin{document}
section{Runtype 1 description}
keywordtable{runtype,param_1}
section{Runtype 2 description}
keywordtable{runtype,param_2}
end{document}
Some remarks
setkeyword{<keyword>}{<description>}
effectively defineskeyw@<keyword>
for you, so that it expands to<definition>
. You can't use this macro directly because its name contains an@
.
getkeyword{<keyword>}
just callskeyw@<keyword>
.- I'm using the e-TeX primitive
detokenize
to print the keyword. This command strips all tokens in its argument of their special meaning (by changing their catcodes), so you can for instance usedetokenize{param_1}
safely. - Without
usepackage[T1]{fontenc}
, underscores are displayed as " ̇", rather than as "_". You'll probably want to use this package anyway because of the reasons outlined here. - I'm using
tabularx
to create a table that has the same width as the current line width. You can replacelinewidth
by some other value if you want a different width (or just usetabular
, in which case you should replace theX
column type by something else).
add a comment |
up vote
1
down vote
It depends on where you want to first key the description you want to repeat.
Probably the best place is in the document preamble or in an external file that you can input
.
documentclass{article}
usepackage{xparse}
% a few line of code for setting up the system
ExplSyntaxOn
NewDocumentCommand{newdesc}{mm}
{% #1 is a key, #2 is the description
prop_gput:Nnn g_nordico_descriptions_plist { #1 } { #2 }
}
NewDocumentCommand{getdesc}{m}
{% #1 is a key
prop_if_in:NnTF g_nordico_descriptions_plist { #1 }
{ prop_item:Nn g_nordico_descriptions_plist { #1 } }
{ ???~non~existent~description~??? }
}
prop_new:N g_nordico_descriptions_plist
ExplSyntaxOff
% the descriptions (they can go in an external file
% say desc.tex and here you'd do input{desc}
newdesc{A}{I don't want to hardcode this description twice...}
begin{document}
section{Runtype 1 description}
begin{center}
begin{tabular}{ l l }
runtype & Keyword to determine runtype. Can be 1 or 2. \
& getdesc{A} \
param_1 & Keyword to determine the parameter of runtype = 1 \
end{tabular}
end{center}
section{Runtype 2 description}
begin{center}
begin{tabular}{ l l }
runtype & Keyword to determine runtype. Can be 1 or 2. \
& getdesc{A} \
param_2 & Keyword to determine the parameter of runtype = 2 \
end{tabular}
end{center}
end{document}
Note that it is not at all necessary to place a tabular
in a floating table
environment (which might make the tabular
go to another page).
add a comment |
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
1
down vote
accepted
documentclass[12pt]{article}
newcommandzzz{%
runtype & Keyword to determine runtype. Can be 1 or 2. \
& I don't want to hardcode this description twice...}
begin{document}
section{Runtype 1 description}
begin{table}[ht]
begin{center}
begin{tabular}{ l l }
zzz\
param_1 & Keyword to determine the parameter of runtype = 1 \
end{tabular}
end{center}
end{table}
section{Runtype 2 description}
begin{table}[ht]
begin{center}
begin{tabular}{ l l }
zzz\
param_2 & Keyword to determine the parameter of runtype = 2 \
end{tabular}
end{center}
end{table}
end{document}
Ok, this works but feels like using a cannon to kill a fly. More precisely: I need to use this with around a hundred keywords, is defining a hundred newcommand to use as a "reference dictionary" advisable? How can I use descriptive names (tablekey_keyname) without using underscores?
– Nordico
9 hours ago
@Nordico As to how to use underscores in a command name, options are limited: 1) change catcode of_
(not recommended, as it breaks subscript math), or 2)csdef
andcsuse
with theetoolbox
package (recommended if you insist on underscore in name)
– Steven B. Segletes
9 hours ago
I mean, not necessarily an underscore, it can be other symbol ("tablekey.keyname") or a way of structuring the commands (like an equivalent to C++ namespaces, so I could do "tablekey::keyname"), anything that makes it more human readable/usable than just altogether "tablekeykeyname".
– Nordico
9 hours ago
1
@Nordico In my example, addcatcode`:=11
as the first line afterdocumentclass
, This changes:
to be a "letter". Thennewcommandzz:z{...}
can be defined and later used aszz:z
. What I don't know is if this breaks anything (e.g., spacing, hyphenation), if you use:
as a normal character.
– Steven B. Segletes
9 hours ago
add a comment |
up vote
1
down vote
accepted
documentclass[12pt]{article}
newcommandzzz{%
runtype & Keyword to determine runtype. Can be 1 or 2. \
& I don't want to hardcode this description twice...}
begin{document}
section{Runtype 1 description}
begin{table}[ht]
begin{center}
begin{tabular}{ l l }
zzz\
param_1 & Keyword to determine the parameter of runtype = 1 \
end{tabular}
end{center}
end{table}
section{Runtype 2 description}
begin{table}[ht]
begin{center}
begin{tabular}{ l l }
zzz\
param_2 & Keyword to determine the parameter of runtype = 2 \
end{tabular}
end{center}
end{table}
end{document}
Ok, this works but feels like using a cannon to kill a fly. More precisely: I need to use this with around a hundred keywords, is defining a hundred newcommand to use as a "reference dictionary" advisable? How can I use descriptive names (tablekey_keyname) without using underscores?
– Nordico
9 hours ago
@Nordico As to how to use underscores in a command name, options are limited: 1) change catcode of_
(not recommended, as it breaks subscript math), or 2)csdef
andcsuse
with theetoolbox
package (recommended if you insist on underscore in name)
– Steven B. Segletes
9 hours ago
I mean, not necessarily an underscore, it can be other symbol ("tablekey.keyname") or a way of structuring the commands (like an equivalent to C++ namespaces, so I could do "tablekey::keyname"), anything that makes it more human readable/usable than just altogether "tablekeykeyname".
– Nordico
9 hours ago
1
@Nordico In my example, addcatcode`:=11
as the first line afterdocumentclass
, This changes:
to be a "letter". Thennewcommandzz:z{...}
can be defined and later used aszz:z
. What I don't know is if this breaks anything (e.g., spacing, hyphenation), if you use:
as a normal character.
– Steven B. Segletes
9 hours ago
add a comment |
up vote
1
down vote
accepted
up vote
1
down vote
accepted
documentclass[12pt]{article}
newcommandzzz{%
runtype & Keyword to determine runtype. Can be 1 or 2. \
& I don't want to hardcode this description twice...}
begin{document}
section{Runtype 1 description}
begin{table}[ht]
begin{center}
begin{tabular}{ l l }
zzz\
param_1 & Keyword to determine the parameter of runtype = 1 \
end{tabular}
end{center}
end{table}
section{Runtype 2 description}
begin{table}[ht]
begin{center}
begin{tabular}{ l l }
zzz\
param_2 & Keyword to determine the parameter of runtype = 2 \
end{tabular}
end{center}
end{table}
end{document}
documentclass[12pt]{article}
newcommandzzz{%
runtype & Keyword to determine runtype. Can be 1 or 2. \
& I don't want to hardcode this description twice...}
begin{document}
section{Runtype 1 description}
begin{table}[ht]
begin{center}
begin{tabular}{ l l }
zzz\
param_1 & Keyword to determine the parameter of runtype = 1 \
end{tabular}
end{center}
end{table}
section{Runtype 2 description}
begin{table}[ht]
begin{center}
begin{tabular}{ l l }
zzz\
param_2 & Keyword to determine the parameter of runtype = 2 \
end{tabular}
end{center}
end{table}
end{document}
answered 10 hours ago
Steven B. Segletes
151k9191398
151k9191398
Ok, this works but feels like using a cannon to kill a fly. More precisely: I need to use this with around a hundred keywords, is defining a hundred newcommand to use as a "reference dictionary" advisable? How can I use descriptive names (tablekey_keyname) without using underscores?
– Nordico
9 hours ago
@Nordico As to how to use underscores in a command name, options are limited: 1) change catcode of_
(not recommended, as it breaks subscript math), or 2)csdef
andcsuse
with theetoolbox
package (recommended if you insist on underscore in name)
– Steven B. Segletes
9 hours ago
I mean, not necessarily an underscore, it can be other symbol ("tablekey.keyname") or a way of structuring the commands (like an equivalent to C++ namespaces, so I could do "tablekey::keyname"), anything that makes it more human readable/usable than just altogether "tablekeykeyname".
– Nordico
9 hours ago
1
@Nordico In my example, addcatcode`:=11
as the first line afterdocumentclass
, This changes:
to be a "letter". Thennewcommandzz:z{...}
can be defined and later used aszz:z
. What I don't know is if this breaks anything (e.g., spacing, hyphenation), if you use:
as a normal character.
– Steven B. Segletes
9 hours ago
add a comment |
Ok, this works but feels like using a cannon to kill a fly. More precisely: I need to use this with around a hundred keywords, is defining a hundred newcommand to use as a "reference dictionary" advisable? How can I use descriptive names (tablekey_keyname) without using underscores?
– Nordico
9 hours ago
@Nordico As to how to use underscores in a command name, options are limited: 1) change catcode of_
(not recommended, as it breaks subscript math), or 2)csdef
andcsuse
with theetoolbox
package (recommended if you insist on underscore in name)
– Steven B. Segletes
9 hours ago
I mean, not necessarily an underscore, it can be other symbol ("tablekey.keyname") or a way of structuring the commands (like an equivalent to C++ namespaces, so I could do "tablekey::keyname"), anything that makes it more human readable/usable than just altogether "tablekeykeyname".
– Nordico
9 hours ago
1
@Nordico In my example, addcatcode`:=11
as the first line afterdocumentclass
, This changes:
to be a "letter". Thennewcommandzz:z{...}
can be defined and later used aszz:z
. What I don't know is if this breaks anything (e.g., spacing, hyphenation), if you use:
as a normal character.
– Steven B. Segletes
9 hours ago
Ok, this works but feels like using a cannon to kill a fly. More precisely: I need to use this with around a hundred keywords, is defining a hundred newcommand to use as a "reference dictionary" advisable? How can I use descriptive names (tablekey_keyname) without using underscores?
– Nordico
9 hours ago
Ok, this works but feels like using a cannon to kill a fly. More precisely: I need to use this with around a hundred keywords, is defining a hundred newcommand to use as a "reference dictionary" advisable? How can I use descriptive names (tablekey_keyname) without using underscores?
– Nordico
9 hours ago
@Nordico As to how to use underscores in a command name, options are limited: 1) change catcode of
_
(not recommended, as it breaks subscript math), or 2) csdef
and csuse
with the etoolbox
package (recommended if you insist on underscore in name)– Steven B. Segletes
9 hours ago
@Nordico As to how to use underscores in a command name, options are limited: 1) change catcode of
_
(not recommended, as it breaks subscript math), or 2) csdef
and csuse
with the etoolbox
package (recommended if you insist on underscore in name)– Steven B. Segletes
9 hours ago
I mean, not necessarily an underscore, it can be other symbol ("tablekey.keyname") or a way of structuring the commands (like an equivalent to C++ namespaces, so I could do "tablekey::keyname"), anything that makes it more human readable/usable than just altogether "tablekeykeyname".
– Nordico
9 hours ago
I mean, not necessarily an underscore, it can be other symbol ("tablekey.keyname") or a way of structuring the commands (like an equivalent to C++ namespaces, so I could do "tablekey::keyname"), anything that makes it more human readable/usable than just altogether "tablekeykeyname".
– Nordico
9 hours ago
1
1
@Nordico In my example, add
catcode`:=11
as the first line after documentclass
, This changes :
to be a "letter". Then newcommandzz:z{...}
can be defined and later used as zz:z
. What I don't know is if this breaks anything (e.g., spacing, hyphenation), if you use :
as a normal character.– Steven B. Segletes
9 hours ago
@Nordico In my example, add
catcode`:=11
as the first line after documentclass
, This changes :
to be a "letter". Then newcommandzz:z{...}
can be defined and later used as zz:z
. What I don't know is if this breaks anything (e.g., spacing, hyphenation), if you use :
as a normal character.– Steven B. Segletes
9 hours ago
add a comment |
up vote
2
down vote
Below, I'm defining the following commands:
setkeyword{<keyword>}{<description>}
can be used to declare a keyword.
getkeyword{<keyword>}
retrieves the description you provided.
keywordtable{<key1>,<key2>,…}
displays a table containing the descriptions corresponding to a comma-separated list of keywords.
(Note that keywords containing underscores are allowed.)
documentclass{article}
usepackage[T1]{fontenc} %% <- necessary for _ to be displayed correctly
usepackage{etoolbox} %% <- for csuse, csdef, ifcsdef and forcsvlist
usepackage{tabularx}
newcommand*keyname[1]{keyw@#1} %% <- just prepends key@
%% Define/retrieve a new keyword:
newcommandsetkeyword[2]{csdef{keyname{#1}}{#2}}
newcommand*getkeyword[1]{%
ifcsdef{keyname{#1}}{% %% <- if the key is defined...
csuse{keyname{#1}}% %% <- return the description
}{% %% <- otherwise...
errmessage{Undefined key: #1}% %% <- ERROR
}%
}
%% Display a table describing a list of keywords:
newcommand*keywordtable[1]{%
begin{tabularx}{linewidth}{lX}
forcsvlist{tableentry}{#1} %% <- apply tableentry to each value in #1
end{tabularx}%
}
newcommand*tableentry[1]{%
formattableentry{detokenize{#1}}{getkeyword{#1}}%
}
newcommandformattableentry[2]{ #1 & #2 \ }
% %% Declaration of keywords:
setkeyword{runtype}{Keyword to determine runtype. Can be 1 or 2. par
I don't want to hardcode this description twiceldots}
setkeyword{param_1}{Keyword to determine the parameter of runtype = 1}
setkeyword{param_2}{Keyword to determine the parameter of runtype = 2}
begin{document}
section{Runtype 1 description}
keywordtable{runtype,param_1}
section{Runtype 2 description}
keywordtable{runtype,param_2}
end{document}
Some remarks
setkeyword{<keyword>}{<description>}
effectively defineskeyw@<keyword>
for you, so that it expands to<definition>
. You can't use this macro directly because its name contains an@
.
getkeyword{<keyword>}
just callskeyw@<keyword>
.- I'm using the e-TeX primitive
detokenize
to print the keyword. This command strips all tokens in its argument of their special meaning (by changing their catcodes), so you can for instance usedetokenize{param_1}
safely. - Without
usepackage[T1]{fontenc}
, underscores are displayed as " ̇", rather than as "_". You'll probably want to use this package anyway because of the reasons outlined here. - I'm using
tabularx
to create a table that has the same width as the current line width. You can replacelinewidth
by some other value if you want a different width (or just usetabular
, in which case you should replace theX
column type by something else).
add a comment |
up vote
2
down vote
Below, I'm defining the following commands:
setkeyword{<keyword>}{<description>}
can be used to declare a keyword.
getkeyword{<keyword>}
retrieves the description you provided.
keywordtable{<key1>,<key2>,…}
displays a table containing the descriptions corresponding to a comma-separated list of keywords.
(Note that keywords containing underscores are allowed.)
documentclass{article}
usepackage[T1]{fontenc} %% <- necessary for _ to be displayed correctly
usepackage{etoolbox} %% <- for csuse, csdef, ifcsdef and forcsvlist
usepackage{tabularx}
newcommand*keyname[1]{keyw@#1} %% <- just prepends key@
%% Define/retrieve a new keyword:
newcommandsetkeyword[2]{csdef{keyname{#1}}{#2}}
newcommand*getkeyword[1]{%
ifcsdef{keyname{#1}}{% %% <- if the key is defined...
csuse{keyname{#1}}% %% <- return the description
}{% %% <- otherwise...
errmessage{Undefined key: #1}% %% <- ERROR
}%
}
%% Display a table describing a list of keywords:
newcommand*keywordtable[1]{%
begin{tabularx}{linewidth}{lX}
forcsvlist{tableentry}{#1} %% <- apply tableentry to each value in #1
end{tabularx}%
}
newcommand*tableentry[1]{%
formattableentry{detokenize{#1}}{getkeyword{#1}}%
}
newcommandformattableentry[2]{ #1 & #2 \ }
% %% Declaration of keywords:
setkeyword{runtype}{Keyword to determine runtype. Can be 1 or 2. par
I don't want to hardcode this description twiceldots}
setkeyword{param_1}{Keyword to determine the parameter of runtype = 1}
setkeyword{param_2}{Keyword to determine the parameter of runtype = 2}
begin{document}
section{Runtype 1 description}
keywordtable{runtype,param_1}
section{Runtype 2 description}
keywordtable{runtype,param_2}
end{document}
Some remarks
setkeyword{<keyword>}{<description>}
effectively defineskeyw@<keyword>
for you, so that it expands to<definition>
. You can't use this macro directly because its name contains an@
.
getkeyword{<keyword>}
just callskeyw@<keyword>
.- I'm using the e-TeX primitive
detokenize
to print the keyword. This command strips all tokens in its argument of their special meaning (by changing their catcodes), so you can for instance usedetokenize{param_1}
safely. - Without
usepackage[T1]{fontenc}
, underscores are displayed as " ̇", rather than as "_". You'll probably want to use this package anyway because of the reasons outlined here. - I'm using
tabularx
to create a table that has the same width as the current line width. You can replacelinewidth
by some other value if you want a different width (or just usetabular
, in which case you should replace theX
column type by something else).
add a comment |
up vote
2
down vote
up vote
2
down vote
Below, I'm defining the following commands:
setkeyword{<keyword>}{<description>}
can be used to declare a keyword.
getkeyword{<keyword>}
retrieves the description you provided.
keywordtable{<key1>,<key2>,…}
displays a table containing the descriptions corresponding to a comma-separated list of keywords.
(Note that keywords containing underscores are allowed.)
documentclass{article}
usepackage[T1]{fontenc} %% <- necessary for _ to be displayed correctly
usepackage{etoolbox} %% <- for csuse, csdef, ifcsdef and forcsvlist
usepackage{tabularx}
newcommand*keyname[1]{keyw@#1} %% <- just prepends key@
%% Define/retrieve a new keyword:
newcommandsetkeyword[2]{csdef{keyname{#1}}{#2}}
newcommand*getkeyword[1]{%
ifcsdef{keyname{#1}}{% %% <- if the key is defined...
csuse{keyname{#1}}% %% <- return the description
}{% %% <- otherwise...
errmessage{Undefined key: #1}% %% <- ERROR
}%
}
%% Display a table describing a list of keywords:
newcommand*keywordtable[1]{%
begin{tabularx}{linewidth}{lX}
forcsvlist{tableentry}{#1} %% <- apply tableentry to each value in #1
end{tabularx}%
}
newcommand*tableentry[1]{%
formattableentry{detokenize{#1}}{getkeyword{#1}}%
}
newcommandformattableentry[2]{ #1 & #2 \ }
% %% Declaration of keywords:
setkeyword{runtype}{Keyword to determine runtype. Can be 1 or 2. par
I don't want to hardcode this description twiceldots}
setkeyword{param_1}{Keyword to determine the parameter of runtype = 1}
setkeyword{param_2}{Keyword to determine the parameter of runtype = 2}
begin{document}
section{Runtype 1 description}
keywordtable{runtype,param_1}
section{Runtype 2 description}
keywordtable{runtype,param_2}
end{document}
Some remarks
setkeyword{<keyword>}{<description>}
effectively defineskeyw@<keyword>
for you, so that it expands to<definition>
. You can't use this macro directly because its name contains an@
.
getkeyword{<keyword>}
just callskeyw@<keyword>
.- I'm using the e-TeX primitive
detokenize
to print the keyword. This command strips all tokens in its argument of their special meaning (by changing their catcodes), so you can for instance usedetokenize{param_1}
safely. - Without
usepackage[T1]{fontenc}
, underscores are displayed as " ̇", rather than as "_". You'll probably want to use this package anyway because of the reasons outlined here. - I'm using
tabularx
to create a table that has the same width as the current line width. You can replacelinewidth
by some other value if you want a different width (or just usetabular
, in which case you should replace theX
column type by something else).
Below, I'm defining the following commands:
setkeyword{<keyword>}{<description>}
can be used to declare a keyword.
getkeyword{<keyword>}
retrieves the description you provided.
keywordtable{<key1>,<key2>,…}
displays a table containing the descriptions corresponding to a comma-separated list of keywords.
(Note that keywords containing underscores are allowed.)
documentclass{article}
usepackage[T1]{fontenc} %% <- necessary for _ to be displayed correctly
usepackage{etoolbox} %% <- for csuse, csdef, ifcsdef and forcsvlist
usepackage{tabularx}
newcommand*keyname[1]{keyw@#1} %% <- just prepends key@
%% Define/retrieve a new keyword:
newcommandsetkeyword[2]{csdef{keyname{#1}}{#2}}
newcommand*getkeyword[1]{%
ifcsdef{keyname{#1}}{% %% <- if the key is defined...
csuse{keyname{#1}}% %% <- return the description
}{% %% <- otherwise...
errmessage{Undefined key: #1}% %% <- ERROR
}%
}
%% Display a table describing a list of keywords:
newcommand*keywordtable[1]{%
begin{tabularx}{linewidth}{lX}
forcsvlist{tableentry}{#1} %% <- apply tableentry to each value in #1
end{tabularx}%
}
newcommand*tableentry[1]{%
formattableentry{detokenize{#1}}{getkeyword{#1}}%
}
newcommandformattableentry[2]{ #1 & #2 \ }
% %% Declaration of keywords:
setkeyword{runtype}{Keyword to determine runtype. Can be 1 or 2. par
I don't want to hardcode this description twiceldots}
setkeyword{param_1}{Keyword to determine the parameter of runtype = 1}
setkeyword{param_2}{Keyword to determine the parameter of runtype = 2}
begin{document}
section{Runtype 1 description}
keywordtable{runtype,param_1}
section{Runtype 2 description}
keywordtable{runtype,param_2}
end{document}
Some remarks
setkeyword{<keyword>}{<description>}
effectively defineskeyw@<keyword>
for you, so that it expands to<definition>
. You can't use this macro directly because its name contains an@
.
getkeyword{<keyword>}
just callskeyw@<keyword>
.- I'm using the e-TeX primitive
detokenize
to print the keyword. This command strips all tokens in its argument of their special meaning (by changing their catcodes), so you can for instance usedetokenize{param_1}
safely. - Without
usepackage[T1]{fontenc}
, underscores are displayed as " ̇", rather than as "_". You'll probably want to use this package anyway because of the reasons outlined here. - I'm using
tabularx
to create a table that has the same width as the current line width. You can replacelinewidth
by some other value if you want a different width (or just usetabular
, in which case you should replace theX
column type by something else).
edited 7 hours ago
Werner
432k609511632
432k609511632
answered 7 hours ago
Circumscribe
2,9171325
2,9171325
add a comment |
add a comment |
up vote
1
down vote
It depends on where you want to first key the description you want to repeat.
Probably the best place is in the document preamble or in an external file that you can input
.
documentclass{article}
usepackage{xparse}
% a few line of code for setting up the system
ExplSyntaxOn
NewDocumentCommand{newdesc}{mm}
{% #1 is a key, #2 is the description
prop_gput:Nnn g_nordico_descriptions_plist { #1 } { #2 }
}
NewDocumentCommand{getdesc}{m}
{% #1 is a key
prop_if_in:NnTF g_nordico_descriptions_plist { #1 }
{ prop_item:Nn g_nordico_descriptions_plist { #1 } }
{ ???~non~existent~description~??? }
}
prop_new:N g_nordico_descriptions_plist
ExplSyntaxOff
% the descriptions (they can go in an external file
% say desc.tex and here you'd do input{desc}
newdesc{A}{I don't want to hardcode this description twice...}
begin{document}
section{Runtype 1 description}
begin{center}
begin{tabular}{ l l }
runtype & Keyword to determine runtype. Can be 1 or 2. \
& getdesc{A} \
param_1 & Keyword to determine the parameter of runtype = 1 \
end{tabular}
end{center}
section{Runtype 2 description}
begin{center}
begin{tabular}{ l l }
runtype & Keyword to determine runtype. Can be 1 or 2. \
& getdesc{A} \
param_2 & Keyword to determine the parameter of runtype = 2 \
end{tabular}
end{center}
end{document}
Note that it is not at all necessary to place a tabular
in a floating table
environment (which might make the tabular
go to another page).
add a comment |
up vote
1
down vote
It depends on where you want to first key the description you want to repeat.
Probably the best place is in the document preamble or in an external file that you can input
.
documentclass{article}
usepackage{xparse}
% a few line of code for setting up the system
ExplSyntaxOn
NewDocumentCommand{newdesc}{mm}
{% #1 is a key, #2 is the description
prop_gput:Nnn g_nordico_descriptions_plist { #1 } { #2 }
}
NewDocumentCommand{getdesc}{m}
{% #1 is a key
prop_if_in:NnTF g_nordico_descriptions_plist { #1 }
{ prop_item:Nn g_nordico_descriptions_plist { #1 } }
{ ???~non~existent~description~??? }
}
prop_new:N g_nordico_descriptions_plist
ExplSyntaxOff
% the descriptions (they can go in an external file
% say desc.tex and here you'd do input{desc}
newdesc{A}{I don't want to hardcode this description twice...}
begin{document}
section{Runtype 1 description}
begin{center}
begin{tabular}{ l l }
runtype & Keyword to determine runtype. Can be 1 or 2. \
& getdesc{A} \
param_1 & Keyword to determine the parameter of runtype = 1 \
end{tabular}
end{center}
section{Runtype 2 description}
begin{center}
begin{tabular}{ l l }
runtype & Keyword to determine runtype. Can be 1 or 2. \
& getdesc{A} \
param_2 & Keyword to determine the parameter of runtype = 2 \
end{tabular}
end{center}
end{document}
Note that it is not at all necessary to place a tabular
in a floating table
environment (which might make the tabular
go to another page).
add a comment |
up vote
1
down vote
up vote
1
down vote
It depends on where you want to first key the description you want to repeat.
Probably the best place is in the document preamble or in an external file that you can input
.
documentclass{article}
usepackage{xparse}
% a few line of code for setting up the system
ExplSyntaxOn
NewDocumentCommand{newdesc}{mm}
{% #1 is a key, #2 is the description
prop_gput:Nnn g_nordico_descriptions_plist { #1 } { #2 }
}
NewDocumentCommand{getdesc}{m}
{% #1 is a key
prop_if_in:NnTF g_nordico_descriptions_plist { #1 }
{ prop_item:Nn g_nordico_descriptions_plist { #1 } }
{ ???~non~existent~description~??? }
}
prop_new:N g_nordico_descriptions_plist
ExplSyntaxOff
% the descriptions (they can go in an external file
% say desc.tex and here you'd do input{desc}
newdesc{A}{I don't want to hardcode this description twice...}
begin{document}
section{Runtype 1 description}
begin{center}
begin{tabular}{ l l }
runtype & Keyword to determine runtype. Can be 1 or 2. \
& getdesc{A} \
param_1 & Keyword to determine the parameter of runtype = 1 \
end{tabular}
end{center}
section{Runtype 2 description}
begin{center}
begin{tabular}{ l l }
runtype & Keyword to determine runtype. Can be 1 or 2. \
& getdesc{A} \
param_2 & Keyword to determine the parameter of runtype = 2 \
end{tabular}
end{center}
end{document}
Note that it is not at all necessary to place a tabular
in a floating table
environment (which might make the tabular
go to another page).
It depends on where you want to first key the description you want to repeat.
Probably the best place is in the document preamble or in an external file that you can input
.
documentclass{article}
usepackage{xparse}
% a few line of code for setting up the system
ExplSyntaxOn
NewDocumentCommand{newdesc}{mm}
{% #1 is a key, #2 is the description
prop_gput:Nnn g_nordico_descriptions_plist { #1 } { #2 }
}
NewDocumentCommand{getdesc}{m}
{% #1 is a key
prop_if_in:NnTF g_nordico_descriptions_plist { #1 }
{ prop_item:Nn g_nordico_descriptions_plist { #1 } }
{ ???~non~existent~description~??? }
}
prop_new:N g_nordico_descriptions_plist
ExplSyntaxOff
% the descriptions (they can go in an external file
% say desc.tex and here you'd do input{desc}
newdesc{A}{I don't want to hardcode this description twice...}
begin{document}
section{Runtype 1 description}
begin{center}
begin{tabular}{ l l }
runtype & Keyword to determine runtype. Can be 1 or 2. \
& getdesc{A} \
param_1 & Keyword to determine the parameter of runtype = 1 \
end{tabular}
end{center}
section{Runtype 2 description}
begin{center}
begin{tabular}{ l l }
runtype & Keyword to determine runtype. Can be 1 or 2. \
& getdesc{A} \
param_2 & Keyword to determine the parameter of runtype = 2 \
end{tabular}
end{center}
end{document}
Note that it is not at all necessary to place a tabular
in a floating table
environment (which might make the tabular
go to another page).
answered 7 hours ago
egreg
700k8518633138
700k8518633138
add a comment |
add a comment |
Nordico is a new contributor. Be nice, and check out our Code of Conduct.
Nordico is a new contributor. Be nice, and check out our Code of Conduct.
Nordico is a new contributor. Be nice, and check out our Code of Conduct.
Nordico is a new contributor. Be nice, and check out our Code of Conduct.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2ftex.stackexchange.com%2fquestions%2f462072%2fhow-to-repeat-the-same-content-in-different-tables%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
1
Welcome to TeX.SE!
– Kurt
10 hours ago
@Kurt Thank you!
– Nordico
9 hours ago