I do precisely this using TeXstudio's macros, and I imagine most editors have a similar macro or snippet functionality.
In TeXstudio, it's as simple as pasting your code into a box and assigning it a name:
- Go to
Macros > Edit Macros.
- Click the
Add button.
- Put your table template text in the
LaTeX Content field, selecting Type = Normal.
- Put the name you want to appear on the TeXstudio menu in the
Name field.
- Optionally, you can define trigger text (
Trigger field) which, when typed, will insert your table template.
To use it, go to the Macros menu in TeXstudio and select the name you added to the Name field above.
In the case of frequently used items, I also assign a shortcut key combination, so inserting, say, a table or figure or section header is as easy as CTRL+SHIFT+T or whatever:
- Go to
Options > Configure TeXstudio....
- Select the
Shortcuts tab.
- Go to the entry for the
Macro menu.
- Expand it and select the macro you just created.
- Double click in the
Current Shortcut field, press your desired key combination, and hit ENTER.
Note that in TeXstudio, shortcuts seem to be positional: if you assign CTRL+Q to your second macro, "Insert Cool Stuff", and then move this macro elsewhere in the editable list, CTRL+Q will activate whatever ends up being in the second position in your macro list, and not "Insert Cool Stuff". This is an odd design choice, so you should be aware of it.
I realize you were looking at using a Latex macro (i.e. \newcommand\whatever) for this purpose, but as is so often the case, Latex makes the impossible easy and the easy excruciating, only solvable through a profound understanding of ancient programming, or both.
Your editor has no such problems ;-)
(EDIT: Considering the plethora of packages --thousands of lines of code, at least-- which have been written just to get basic table functionality to work right in Latex (by-row formatting, multi-columns, expanding cells, vertical alignment settings, and even that most basic of all functions, text wrapping, which worked just fine on my Atari 800 without any hacks), I stand by my statement about having to have a deep knowledge of ancient programming to do what is easy in other software (and any language that treats _ as a reserved character is ancient, if not pre-historic). That doesn't mean Latex is bad, but it is perhaps the single most arcane thing anyone not working on legacy bank mainframes will ever come across).
{l|ll||l}or{l|l|l||l}? Are there always at least 3 columns? What should happen if there are less? – Skillmon Oct 23 '17 at 08:28