Command for nulifying spaces (String.trim()) in latex Announcing the arrival of Valued Associate #679: Cesar Manara Planned maintenance scheduled April 17/18, 2019 at 00:00UTC (8:00pm US/Eastern)Backward compatibility for command namesHow to make spaces as command argument delimiters?Expandable 'character scanning' command that preserves spacesCreate command to inline an image in a question?LaTeX command for equal (=)LaTeX Document class for department technical reportsFindig the latex-command for a characterlooking for selective latex command replacement methodsEnvironment for respecting spacesSpaces around command with empty output (for comments within paragraphs)
Identifying polygons that intersect with another layer using QGIS?
How do I keep my slimes from escaping their pens?
What is a non-alternating simple group with big order, but relatively few conjugacy classes?
Bete Noir -- no dairy
String `!23` is replaced with `docker` in command line
Why do people hide their license plates in the EU?
Resolving to minmaj7
What is the meaning of the new sigil in Game of Thrones Season 8 intro?
Is it ethical to give a final exam after the professor has quit before teaching the remaining chapters of the course?
porting install scripts : can rpm replace apt?
Should I use a zero-interest credit card for a large one-time purchase?
Extract all GPU name, model and GPU ram
What is Arya's weapon design?
How to call a function with default parameter through a pointer to function that is the return of another function?
How to react to hostile behavior from a senior developer?
What causes the vertical darker bands in my photo?
How does debian/ubuntu knows a package has a updated version
Identify plant with long narrow paired leaves and reddish stems
Short Story with Cinderella as a Voo-doo Witch
What's the purpose of writing one's academic biography in the third person?
What does "fit" mean in this sentence?
Can a non-EU citizen traveling with me come with me through the EU passport line?
When do you get frequent flier miles - when you buy, or when you fly?
Why did the Falcon Heavy center core fall off the ASDS OCISLY barge?
Command for nulifying spaces (String.trim()) in latex
Announcing the arrival of Valued Associate #679: Cesar Manara
Planned maintenance scheduled April 17/18, 2019 at 00:00UTC (8:00pm US/Eastern)Backward compatibility for command namesHow to make spaces as command argument delimiters?Expandable 'character scanning' command that preserves spacesCreate command to inline an image in a question?LaTeX command for equal (=)LaTeX Document class for department technical reportsFindig the latex-command for a characterlooking for selective latex command replacement methodsEnvironment for respecting spacesSpaces around command with empty output (for comments within paragraphs)
I want to declare a variable in class that takes in the font name from the preamble of the TeX
file.
Something like docFont<Variable>
. Is there a command that could nullify the space key in latex? For example docFont Arial
would return the same result as docFontArial
.
My myclass.cls
File is something like this:
NeedsTeXFormatLaTeX2e
ProvidesClassmyclass[2019/04/09 Standard HomeWork Template]
LoadClass[a4paper,12pt,onecolumn,final]article
RequirePackgeetoolbox
defhFont#1gdef@hFont#1
def@hFont@latex@warning@no@lineNo noexpandhFont given
BeforeBeginEnvironmentdocument
RequirePackagexepersian
settextfont@hFont
And my *.Tex
file is something like this:
documentclassmyclass
hFontArial %Document Persian Font
begindocument
something
enddocument
macros documentclass-writing
add a comment |
I want to declare a variable in class that takes in the font name from the preamble of the TeX
file.
Something like docFont<Variable>
. Is there a command that could nullify the space key in latex? For example docFont Arial
would return the same result as docFontArial
.
My myclass.cls
File is something like this:
NeedsTeXFormatLaTeX2e
ProvidesClassmyclass[2019/04/09 Standard HomeWork Template]
LoadClass[a4paper,12pt,onecolumn,final]article
RequirePackgeetoolbox
defhFont#1gdef@hFont#1
def@hFont@latex@warning@no@lineNo noexpandhFont given
BeforeBeginEnvironmentdocument
RequirePackagexepersian
settextfont@hFont
And my *.Tex
file is something like this:
documentclassmyclass
hFontArial %Document Persian Font
begindocument
something
enddocument
macros documentclass-writing
2
Are you talking about some sort ofstring.Trim()
you would find in other languages? You can check outtl_trim_spaces:n
from expl3 –texdoc interface3
has the documentation.
– Sean Allred
Apr 11 at 10:00
@SeanAllred This is exactly what I am looking for
– Al_Fh
Apr 11 at 10:00
2
Can you expand your question with a complete MWE? Due to expansion concerns, the exact context of how you want to use this macro will determine its implementation.
– Sean Allred
Apr 11 at 10:02
@SeanAllred edited.
– Al_Fh
Apr 11 at 10:13
add a comment |
I want to declare a variable in class that takes in the font name from the preamble of the TeX
file.
Something like docFont<Variable>
. Is there a command that could nullify the space key in latex? For example docFont Arial
would return the same result as docFontArial
.
My myclass.cls
File is something like this:
NeedsTeXFormatLaTeX2e
ProvidesClassmyclass[2019/04/09 Standard HomeWork Template]
LoadClass[a4paper,12pt,onecolumn,final]article
RequirePackgeetoolbox
defhFont#1gdef@hFont#1
def@hFont@latex@warning@no@lineNo noexpandhFont given
BeforeBeginEnvironmentdocument
RequirePackagexepersian
settextfont@hFont
And my *.Tex
file is something like this:
documentclassmyclass
hFontArial %Document Persian Font
begindocument
something
enddocument
macros documentclass-writing
I want to declare a variable in class that takes in the font name from the preamble of the TeX
file.
Something like docFont<Variable>
. Is there a command that could nullify the space key in latex? For example docFont Arial
would return the same result as docFontArial
.
My myclass.cls
File is something like this:
NeedsTeXFormatLaTeX2e
ProvidesClassmyclass[2019/04/09 Standard HomeWork Template]
LoadClass[a4paper,12pt,onecolumn,final]article
RequirePackgeetoolbox
defhFont#1gdef@hFont#1
def@hFont@latex@warning@no@lineNo noexpandhFont given
BeforeBeginEnvironmentdocument
RequirePackagexepersian
settextfont@hFont
And my *.Tex
file is something like this:
documentclassmyclass
hFontArial %Document Persian Font
begindocument
something
enddocument
macros documentclass-writing
macros documentclass-writing
edited Apr 12 at 11:19
Al_Fh
asked Apr 11 at 9:53
Al_FhAl_Fh
997
997
2
Are you talking about some sort ofstring.Trim()
you would find in other languages? You can check outtl_trim_spaces:n
from expl3 –texdoc interface3
has the documentation.
– Sean Allred
Apr 11 at 10:00
@SeanAllred This is exactly what I am looking for
– Al_Fh
Apr 11 at 10:00
2
Can you expand your question with a complete MWE? Due to expansion concerns, the exact context of how you want to use this macro will determine its implementation.
– Sean Allred
Apr 11 at 10:02
@SeanAllred edited.
– Al_Fh
Apr 11 at 10:13
add a comment |
2
Are you talking about some sort ofstring.Trim()
you would find in other languages? You can check outtl_trim_spaces:n
from expl3 –texdoc interface3
has the documentation.
– Sean Allred
Apr 11 at 10:00
@SeanAllred This is exactly what I am looking for
– Al_Fh
Apr 11 at 10:00
2
Can you expand your question with a complete MWE? Due to expansion concerns, the exact context of how you want to use this macro will determine its implementation.
– Sean Allred
Apr 11 at 10:02
@SeanAllred edited.
– Al_Fh
Apr 11 at 10:13
2
2
Are you talking about some sort of
string.Trim()
you would find in other languages? You can check out tl_trim_spaces:n
from expl3 – texdoc interface3
has the documentation.– Sean Allred
Apr 11 at 10:00
Are you talking about some sort of
string.Trim()
you would find in other languages? You can check out tl_trim_spaces:n
from expl3 – texdoc interface3
has the documentation.– Sean Allred
Apr 11 at 10:00
@SeanAllred This is exactly what I am looking for
– Al_Fh
Apr 11 at 10:00
@SeanAllred This is exactly what I am looking for
– Al_Fh
Apr 11 at 10:00
2
2
Can you expand your question with a complete MWE? Due to expansion concerns, the exact context of how you want to use this macro will determine its implementation.
– Sean Allred
Apr 11 at 10:02
Can you expand your question with a complete MWE? Due to expansion concerns, the exact context of how you want to use this macro will determine its implementation.
– Sean Allred
Apr 11 at 10:02
@SeanAllred edited.
– Al_Fh
Apr 11 at 10:13
@SeanAllred edited.
– Al_Fh
Apr 11 at 10:13
add a comment |
2 Answers
2
active
oldest
votes
You can use the trimspaces package for this purpose.
Simple examples
documentclassarticle
usepackagetrimspaces
makeatletter
newcommand*trim[1]%
trim@spaces@noexp#1%
makeatother
begindocument
textbar trima bcd eftextbar
textbar trim a bcd ef textbar
enddocument
Here is another way that first stores the result of the trimming operation in a macro (my@tmp@trimmed
), then expands this macro:
documentclassarticle
usepackagetrimspaces
makeatletter
newcommand*trim[1]%
begingroup
edefmy@tmp@trimmedtrim@spaces@noexp#1%
expandafter
endgroup
my@tmp@trimmed
makeatother
begindocument
Xtrima bc defY
Xtrim a bc def Y
enddocument
Overview of the possibilities
The trimspaces package offers several variants depending on what you want:
- left-only, right-only or both-sides trimming (left:
trim@pre@space
andtrim@pre@space@noexp
; right:trim@post@space
,trim@post@space@noexp
; both:trim@spaces
,trim@spaces@noexp
); - whether you want to expand tokens while trimming, or not (so far, I chose the
@noexp
variant in the examples, that just trims without expanding; otherwise, seetrim@spaces
in the package documentation and below).
It also allows one to trim the contents of an already-existing macro. Here is the example from the documentation concerning this point:
defb b foo !
trim@spaces@inb
is equivalent to
defbb foo !
trim@pre@space@in
and trim@post@space@in
offer analoguous functionality for left-only, resp. right-only trimming.
Difference between trim@spaces
and trim@spaces@noexp
The following example illustrates the functional difference between trim@spaces
and trim@spaces@noexp
1: the former expands tokens from its argument whereas the latter doesn't (they can then be expanded “at the last moment”, which is often desirable).
documentclassarticle
usepackagetrimspaces
makeatletter
newcommand*storeTrimmedImmediateExp[2]%
xdef#1trim@spaces#2%
newcommand*storeTrimmedNoExp[2]%
xdef#1trim@spaces@noexp#2%
makeatother
newcommand*foo[default foo]
storeTrimmedImmediateExpimmediateExp afoo b % foo is expanded now
storeTrimmedNoExpdelayedExp afoo b % foo is not expanded yet
renewcommand*foo[updated foo]
begindocument
textbar immediateExp textbar % there is no foo inside immediateExp
textbar delayedExp textbar % foo is expanded after delayedExp
enddocument
Corner cases
This last example explores a few corner cases with various kinds of spaces:
documentclassarticle
usepackagetrimspaces
makeatletter
newcommand*trim[1]%
trim@spaces@noexp#1%
makeatother
% Define implicitSpace to be let-equal to a space token
begingroup
def\globalletimplicitSpace= \ % cf. TeXbook exercise 24.6
endgroup
begindocument
Atrima bc defB
Ctrim a bc def D
Etrim a bc def F % control spaces aren't trimmed
% Implicit space tokens are... sometimes trimmed (?)
GtrimimplicitSpace a bc defimplicitSpaceH
enddocument
Footnote
- The other difference being that
trim@spaces@noexp
requires an eTeX engine, whiletrim@spaces
doesn't.
Simple and easy to grasp. Thank you very much.
– Al_Fh
Apr 11 at 10:39
1
I'm glad it helped you. I realized there was an even simpler method, therefore I have expanded my answer starting with that one, then showing the difference betweentrim@spaces
andtrim@spaces@noexp
, and finally testing the behavior oftrim@spaces@noexp
with various kinds of spaces.
– frougon
Apr 11 at 22:12
add a comment |
Here, I preprocess the argument to docfont
and pass the result to docfontcore
, which solely for the purposes of demonstration, detokenizes its argument, so that you can see that it gives the originally specified tokens, trimmed of leading and trailing (but not intermediate) spaces.
The last MWE example shows that passing macros poses no impediment to the approach.
documentclassarticle
usepackage[T1]fontenc
usepackagelistofitems
newcommanddocfont[1]%
setsepchar!!!!% USE SOMETHING NOT IN THE FONTNAMES
readlist*trimmedvar#1%
itemtomacrotrimmedvar[1]thetrimmedvar%
expandafterdocfontcoreexpandafterthetrimmedvar%
newcommanddocfontcore[1][detokenize#1]
begindocument
docfont Arial Bold
docfontArial Bold
docfontArial Bold
newcommandmyfontPalatino
docfont myfont-oblique
enddocument
Thank you very much. I will start reading more into this later. Much appreciated.
– Al_Fh
Apr 11 at 10:39
add a comment |
Your Answer
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "85"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
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%2f484288%2fcommand-for-nulifying-spaces-string-trim-in-latex%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
You can use the trimspaces package for this purpose.
Simple examples
documentclassarticle
usepackagetrimspaces
makeatletter
newcommand*trim[1]%
trim@spaces@noexp#1%
makeatother
begindocument
textbar trima bcd eftextbar
textbar trim a bcd ef textbar
enddocument
Here is another way that first stores the result of the trimming operation in a macro (my@tmp@trimmed
), then expands this macro:
documentclassarticle
usepackagetrimspaces
makeatletter
newcommand*trim[1]%
begingroup
edefmy@tmp@trimmedtrim@spaces@noexp#1%
expandafter
endgroup
my@tmp@trimmed
makeatother
begindocument
Xtrima bc defY
Xtrim a bc def Y
enddocument
Overview of the possibilities
The trimspaces package offers several variants depending on what you want:
- left-only, right-only or both-sides trimming (left:
trim@pre@space
andtrim@pre@space@noexp
; right:trim@post@space
,trim@post@space@noexp
; both:trim@spaces
,trim@spaces@noexp
); - whether you want to expand tokens while trimming, or not (so far, I chose the
@noexp
variant in the examples, that just trims without expanding; otherwise, seetrim@spaces
in the package documentation and below).
It also allows one to trim the contents of an already-existing macro. Here is the example from the documentation concerning this point:
defb b foo !
trim@spaces@inb
is equivalent to
defbb foo !
trim@pre@space@in
and trim@post@space@in
offer analoguous functionality for left-only, resp. right-only trimming.
Difference between trim@spaces
and trim@spaces@noexp
The following example illustrates the functional difference between trim@spaces
and trim@spaces@noexp
1: the former expands tokens from its argument whereas the latter doesn't (they can then be expanded “at the last moment”, which is often desirable).
documentclassarticle
usepackagetrimspaces
makeatletter
newcommand*storeTrimmedImmediateExp[2]%
xdef#1trim@spaces#2%
newcommand*storeTrimmedNoExp[2]%
xdef#1trim@spaces@noexp#2%
makeatother
newcommand*foo[default foo]
storeTrimmedImmediateExpimmediateExp afoo b % foo is expanded now
storeTrimmedNoExpdelayedExp afoo b % foo is not expanded yet
renewcommand*foo[updated foo]
begindocument
textbar immediateExp textbar % there is no foo inside immediateExp
textbar delayedExp textbar % foo is expanded after delayedExp
enddocument
Corner cases
This last example explores a few corner cases with various kinds of spaces:
documentclassarticle
usepackagetrimspaces
makeatletter
newcommand*trim[1]%
trim@spaces@noexp#1%
makeatother
% Define implicitSpace to be let-equal to a space token
begingroup
def\globalletimplicitSpace= \ % cf. TeXbook exercise 24.6
endgroup
begindocument
Atrima bc defB
Ctrim a bc def D
Etrim a bc def F % control spaces aren't trimmed
% Implicit space tokens are... sometimes trimmed (?)
GtrimimplicitSpace a bc defimplicitSpaceH
enddocument
Footnote
- The other difference being that
trim@spaces@noexp
requires an eTeX engine, whiletrim@spaces
doesn't.
Simple and easy to grasp. Thank you very much.
– Al_Fh
Apr 11 at 10:39
1
I'm glad it helped you. I realized there was an even simpler method, therefore I have expanded my answer starting with that one, then showing the difference betweentrim@spaces
andtrim@spaces@noexp
, and finally testing the behavior oftrim@spaces@noexp
with various kinds of spaces.
– frougon
Apr 11 at 22:12
add a comment |
You can use the trimspaces package for this purpose.
Simple examples
documentclassarticle
usepackagetrimspaces
makeatletter
newcommand*trim[1]%
trim@spaces@noexp#1%
makeatother
begindocument
textbar trima bcd eftextbar
textbar trim a bcd ef textbar
enddocument
Here is another way that first stores the result of the trimming operation in a macro (my@tmp@trimmed
), then expands this macro:
documentclassarticle
usepackagetrimspaces
makeatletter
newcommand*trim[1]%
begingroup
edefmy@tmp@trimmedtrim@spaces@noexp#1%
expandafter
endgroup
my@tmp@trimmed
makeatother
begindocument
Xtrima bc defY
Xtrim a bc def Y
enddocument
Overview of the possibilities
The trimspaces package offers several variants depending on what you want:
- left-only, right-only or both-sides trimming (left:
trim@pre@space
andtrim@pre@space@noexp
; right:trim@post@space
,trim@post@space@noexp
; both:trim@spaces
,trim@spaces@noexp
); - whether you want to expand tokens while trimming, or not (so far, I chose the
@noexp
variant in the examples, that just trims without expanding; otherwise, seetrim@spaces
in the package documentation and below).
It also allows one to trim the contents of an already-existing macro. Here is the example from the documentation concerning this point:
defb b foo !
trim@spaces@inb
is equivalent to
defbb foo !
trim@pre@space@in
and trim@post@space@in
offer analoguous functionality for left-only, resp. right-only trimming.
Difference between trim@spaces
and trim@spaces@noexp
The following example illustrates the functional difference between trim@spaces
and trim@spaces@noexp
1: the former expands tokens from its argument whereas the latter doesn't (they can then be expanded “at the last moment”, which is often desirable).
documentclassarticle
usepackagetrimspaces
makeatletter
newcommand*storeTrimmedImmediateExp[2]%
xdef#1trim@spaces#2%
newcommand*storeTrimmedNoExp[2]%
xdef#1trim@spaces@noexp#2%
makeatother
newcommand*foo[default foo]
storeTrimmedImmediateExpimmediateExp afoo b % foo is expanded now
storeTrimmedNoExpdelayedExp afoo b % foo is not expanded yet
renewcommand*foo[updated foo]
begindocument
textbar immediateExp textbar % there is no foo inside immediateExp
textbar delayedExp textbar % foo is expanded after delayedExp
enddocument
Corner cases
This last example explores a few corner cases with various kinds of spaces:
documentclassarticle
usepackagetrimspaces
makeatletter
newcommand*trim[1]%
trim@spaces@noexp#1%
makeatother
% Define implicitSpace to be let-equal to a space token
begingroup
def\globalletimplicitSpace= \ % cf. TeXbook exercise 24.6
endgroup
begindocument
Atrima bc defB
Ctrim a bc def D
Etrim a bc def F % control spaces aren't trimmed
% Implicit space tokens are... sometimes trimmed (?)
GtrimimplicitSpace a bc defimplicitSpaceH
enddocument
Footnote
- The other difference being that
trim@spaces@noexp
requires an eTeX engine, whiletrim@spaces
doesn't.
Simple and easy to grasp. Thank you very much.
– Al_Fh
Apr 11 at 10:39
1
I'm glad it helped you. I realized there was an even simpler method, therefore I have expanded my answer starting with that one, then showing the difference betweentrim@spaces
andtrim@spaces@noexp
, and finally testing the behavior oftrim@spaces@noexp
with various kinds of spaces.
– frougon
Apr 11 at 22:12
add a comment |
You can use the trimspaces package for this purpose.
Simple examples
documentclassarticle
usepackagetrimspaces
makeatletter
newcommand*trim[1]%
trim@spaces@noexp#1%
makeatother
begindocument
textbar trima bcd eftextbar
textbar trim a bcd ef textbar
enddocument
Here is another way that first stores the result of the trimming operation in a macro (my@tmp@trimmed
), then expands this macro:
documentclassarticle
usepackagetrimspaces
makeatletter
newcommand*trim[1]%
begingroup
edefmy@tmp@trimmedtrim@spaces@noexp#1%
expandafter
endgroup
my@tmp@trimmed
makeatother
begindocument
Xtrima bc defY
Xtrim a bc def Y
enddocument
Overview of the possibilities
The trimspaces package offers several variants depending on what you want:
- left-only, right-only or both-sides trimming (left:
trim@pre@space
andtrim@pre@space@noexp
; right:trim@post@space
,trim@post@space@noexp
; both:trim@spaces
,trim@spaces@noexp
); - whether you want to expand tokens while trimming, or not (so far, I chose the
@noexp
variant in the examples, that just trims without expanding; otherwise, seetrim@spaces
in the package documentation and below).
It also allows one to trim the contents of an already-existing macro. Here is the example from the documentation concerning this point:
defb b foo !
trim@spaces@inb
is equivalent to
defbb foo !
trim@pre@space@in
and trim@post@space@in
offer analoguous functionality for left-only, resp. right-only trimming.
Difference between trim@spaces
and trim@spaces@noexp
The following example illustrates the functional difference between trim@spaces
and trim@spaces@noexp
1: the former expands tokens from its argument whereas the latter doesn't (they can then be expanded “at the last moment”, which is often desirable).
documentclassarticle
usepackagetrimspaces
makeatletter
newcommand*storeTrimmedImmediateExp[2]%
xdef#1trim@spaces#2%
newcommand*storeTrimmedNoExp[2]%
xdef#1trim@spaces@noexp#2%
makeatother
newcommand*foo[default foo]
storeTrimmedImmediateExpimmediateExp afoo b % foo is expanded now
storeTrimmedNoExpdelayedExp afoo b % foo is not expanded yet
renewcommand*foo[updated foo]
begindocument
textbar immediateExp textbar % there is no foo inside immediateExp
textbar delayedExp textbar % foo is expanded after delayedExp
enddocument
Corner cases
This last example explores a few corner cases with various kinds of spaces:
documentclassarticle
usepackagetrimspaces
makeatletter
newcommand*trim[1]%
trim@spaces@noexp#1%
makeatother
% Define implicitSpace to be let-equal to a space token
begingroup
def\globalletimplicitSpace= \ % cf. TeXbook exercise 24.6
endgroup
begindocument
Atrima bc defB
Ctrim a bc def D
Etrim a bc def F % control spaces aren't trimmed
% Implicit space tokens are... sometimes trimmed (?)
GtrimimplicitSpace a bc defimplicitSpaceH
enddocument
Footnote
- The other difference being that
trim@spaces@noexp
requires an eTeX engine, whiletrim@spaces
doesn't.
You can use the trimspaces package for this purpose.
Simple examples
documentclassarticle
usepackagetrimspaces
makeatletter
newcommand*trim[1]%
trim@spaces@noexp#1%
makeatother
begindocument
textbar trima bcd eftextbar
textbar trim a bcd ef textbar
enddocument
Here is another way that first stores the result of the trimming operation in a macro (my@tmp@trimmed
), then expands this macro:
documentclassarticle
usepackagetrimspaces
makeatletter
newcommand*trim[1]%
begingroup
edefmy@tmp@trimmedtrim@spaces@noexp#1%
expandafter
endgroup
my@tmp@trimmed
makeatother
begindocument
Xtrima bc defY
Xtrim a bc def Y
enddocument
Overview of the possibilities
The trimspaces package offers several variants depending on what you want:
- left-only, right-only or both-sides trimming (left:
trim@pre@space
andtrim@pre@space@noexp
; right:trim@post@space
,trim@post@space@noexp
; both:trim@spaces
,trim@spaces@noexp
); - whether you want to expand tokens while trimming, or not (so far, I chose the
@noexp
variant in the examples, that just trims without expanding; otherwise, seetrim@spaces
in the package documentation and below).
It also allows one to trim the contents of an already-existing macro. Here is the example from the documentation concerning this point:
defb b foo !
trim@spaces@inb
is equivalent to
defbb foo !
trim@pre@space@in
and trim@post@space@in
offer analoguous functionality for left-only, resp. right-only trimming.
Difference between trim@spaces
and trim@spaces@noexp
The following example illustrates the functional difference between trim@spaces
and trim@spaces@noexp
1: the former expands tokens from its argument whereas the latter doesn't (they can then be expanded “at the last moment”, which is often desirable).
documentclassarticle
usepackagetrimspaces
makeatletter
newcommand*storeTrimmedImmediateExp[2]%
xdef#1trim@spaces#2%
newcommand*storeTrimmedNoExp[2]%
xdef#1trim@spaces@noexp#2%
makeatother
newcommand*foo[default foo]
storeTrimmedImmediateExpimmediateExp afoo b % foo is expanded now
storeTrimmedNoExpdelayedExp afoo b % foo is not expanded yet
renewcommand*foo[updated foo]
begindocument
textbar immediateExp textbar % there is no foo inside immediateExp
textbar delayedExp textbar % foo is expanded after delayedExp
enddocument
Corner cases
This last example explores a few corner cases with various kinds of spaces:
documentclassarticle
usepackagetrimspaces
makeatletter
newcommand*trim[1]%
trim@spaces@noexp#1%
makeatother
% Define implicitSpace to be let-equal to a space token
begingroup
def\globalletimplicitSpace= \ % cf. TeXbook exercise 24.6
endgroup
begindocument
Atrima bc defB
Ctrim a bc def D
Etrim a bc def F % control spaces aren't trimmed
% Implicit space tokens are... sometimes trimmed (?)
GtrimimplicitSpace a bc defimplicitSpaceH
enddocument
Footnote
- The other difference being that
trim@spaces@noexp
requires an eTeX engine, whiletrim@spaces
doesn't.
edited Apr 12 at 7:08
answered Apr 11 at 10:07
frougonfrougon
1,054712
1,054712
Simple and easy to grasp. Thank you very much.
– Al_Fh
Apr 11 at 10:39
1
I'm glad it helped you. I realized there was an even simpler method, therefore I have expanded my answer starting with that one, then showing the difference betweentrim@spaces
andtrim@spaces@noexp
, and finally testing the behavior oftrim@spaces@noexp
with various kinds of spaces.
– frougon
Apr 11 at 22:12
add a comment |
Simple and easy to grasp. Thank you very much.
– Al_Fh
Apr 11 at 10:39
1
I'm glad it helped you. I realized there was an even simpler method, therefore I have expanded my answer starting with that one, then showing the difference betweentrim@spaces
andtrim@spaces@noexp
, and finally testing the behavior oftrim@spaces@noexp
with various kinds of spaces.
– frougon
Apr 11 at 22:12
Simple and easy to grasp. Thank you very much.
– Al_Fh
Apr 11 at 10:39
Simple and easy to grasp. Thank you very much.
– Al_Fh
Apr 11 at 10:39
1
1
I'm glad it helped you. I realized there was an even simpler method, therefore I have expanded my answer starting with that one, then showing the difference between
trim@spaces
and trim@spaces@noexp
, and finally testing the behavior of trim@spaces@noexp
with various kinds of spaces.– frougon
Apr 11 at 22:12
I'm glad it helped you. I realized there was an even simpler method, therefore I have expanded my answer starting with that one, then showing the difference between
trim@spaces
and trim@spaces@noexp
, and finally testing the behavior of trim@spaces@noexp
with various kinds of spaces.– frougon
Apr 11 at 22:12
add a comment |
Here, I preprocess the argument to docfont
and pass the result to docfontcore
, which solely for the purposes of demonstration, detokenizes its argument, so that you can see that it gives the originally specified tokens, trimmed of leading and trailing (but not intermediate) spaces.
The last MWE example shows that passing macros poses no impediment to the approach.
documentclassarticle
usepackage[T1]fontenc
usepackagelistofitems
newcommanddocfont[1]%
setsepchar!!!!% USE SOMETHING NOT IN THE FONTNAMES
readlist*trimmedvar#1%
itemtomacrotrimmedvar[1]thetrimmedvar%
expandafterdocfontcoreexpandafterthetrimmedvar%
newcommanddocfontcore[1][detokenize#1]
begindocument
docfont Arial Bold
docfontArial Bold
docfontArial Bold
newcommandmyfontPalatino
docfont myfont-oblique
enddocument
Thank you very much. I will start reading more into this later. Much appreciated.
– Al_Fh
Apr 11 at 10:39
add a comment |
Here, I preprocess the argument to docfont
and pass the result to docfontcore
, which solely for the purposes of demonstration, detokenizes its argument, so that you can see that it gives the originally specified tokens, trimmed of leading and trailing (but not intermediate) spaces.
The last MWE example shows that passing macros poses no impediment to the approach.
documentclassarticle
usepackage[T1]fontenc
usepackagelistofitems
newcommanddocfont[1]%
setsepchar!!!!% USE SOMETHING NOT IN THE FONTNAMES
readlist*trimmedvar#1%
itemtomacrotrimmedvar[1]thetrimmedvar%
expandafterdocfontcoreexpandafterthetrimmedvar%
newcommanddocfontcore[1][detokenize#1]
begindocument
docfont Arial Bold
docfontArial Bold
docfontArial Bold
newcommandmyfontPalatino
docfont myfont-oblique
enddocument
Thank you very much. I will start reading more into this later. Much appreciated.
– Al_Fh
Apr 11 at 10:39
add a comment |
Here, I preprocess the argument to docfont
and pass the result to docfontcore
, which solely for the purposes of demonstration, detokenizes its argument, so that you can see that it gives the originally specified tokens, trimmed of leading and trailing (but not intermediate) spaces.
The last MWE example shows that passing macros poses no impediment to the approach.
documentclassarticle
usepackage[T1]fontenc
usepackagelistofitems
newcommanddocfont[1]%
setsepchar!!!!% USE SOMETHING NOT IN THE FONTNAMES
readlist*trimmedvar#1%
itemtomacrotrimmedvar[1]thetrimmedvar%
expandafterdocfontcoreexpandafterthetrimmedvar%
newcommanddocfontcore[1][detokenize#1]
begindocument
docfont Arial Bold
docfontArial Bold
docfontArial Bold
newcommandmyfontPalatino
docfont myfont-oblique
enddocument
Here, I preprocess the argument to docfont
and pass the result to docfontcore
, which solely for the purposes of demonstration, detokenizes its argument, so that you can see that it gives the originally specified tokens, trimmed of leading and trailing (but not intermediate) spaces.
The last MWE example shows that passing macros poses no impediment to the approach.
documentclassarticle
usepackage[T1]fontenc
usepackagelistofitems
newcommanddocfont[1]%
setsepchar!!!!% USE SOMETHING NOT IN THE FONTNAMES
readlist*trimmedvar#1%
itemtomacrotrimmedvar[1]thetrimmedvar%
expandafterdocfontcoreexpandafterthetrimmedvar%
newcommanddocfontcore[1][detokenize#1]
begindocument
docfont Arial Bold
docfontArial Bold
docfontArial Bold
newcommandmyfontPalatino
docfont myfont-oblique
enddocument
edited Apr 11 at 10:19
answered Apr 11 at 10:07
Steven B. SegletesSteven B. Segletes
162k9206418
162k9206418
Thank you very much. I will start reading more into this later. Much appreciated.
– Al_Fh
Apr 11 at 10:39
add a comment |
Thank you very much. I will start reading more into this later. Much appreciated.
– Al_Fh
Apr 11 at 10:39
Thank you very much. I will start reading more into this later. Much appreciated.
– Al_Fh
Apr 11 at 10:39
Thank you very much. I will start reading more into this later. Much appreciated.
– Al_Fh
Apr 11 at 10:39
add a comment |
Thanks for contributing an answer to TeX - LaTeX Stack Exchange!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
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%2f484288%2fcommand-for-nulifying-spaces-string-trim-in-latex%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
2
Are you talking about some sort of
string.Trim()
you would find in other languages? You can check outtl_trim_spaces:n
from expl3 –texdoc interface3
has the documentation.– Sean Allred
Apr 11 at 10:00
@SeanAllred This is exactly what I am looking for
– Al_Fh
Apr 11 at 10:00
2
Can you expand your question with a complete MWE? Due to expansion concerns, the exact context of how you want to use this macro will determine its implementation.
– Sean Allred
Apr 11 at 10:02
@SeanAllred edited.
– Al_Fh
Apr 11 at 10:13