Use xstring to substitute reformatted names in strings












0















I would like to replace occurrences of strings conditionally within larger strings, using the IfSubStr and StrSubstitute macros from the xstring package. The issue that I'm running into is related to expansion of the makebold macro, which is a function of surnamefirst and surnamelast macro expansion. How can I get the makebold macro to work correctly?



documentclass{article}

usepackage{xstring}

newcommand{name}{John Doe}

makeatletter

% Extract last name from name
newcommand{lastname}{%
IfSubStr{name}{.}%
{StrBehind{name}{. }}%
{StrBehind{name}{ }}%
[@lastname]
}

% Extract first name from name
newcommand{firstname}{%
StrBefore{name}{ }[@firstname]
}

% Extract first initial from name
newcommand{firstinitial}{%
StrLeft{name}{1}[@firstinitial]
}

% Extract middle initial from name
newcommand{middleinitial}{%
StrBetween{name}{ }{.}[@middleinitial]
}

newcommand{testxstring}{%
middleinitial % Find middle initial
IfStrEq{@middleinitial}{}%
{No middle initial!}%
{Middle initial: @middleinitial}
}

% Surname first
newcommand{surnamefirst}{%
firstinitial
middleinitial % Find middle initial
lastname
IfStrEq{@middleinitial}{}%
{@lastname, @firstinitial.}%
{@lastname, @firstinitial.~@middleinitial.}
}

% Surname last
newcommand{surnamelast}{%
firstinitial
middleinitial % Find middle initial
lastname
IfStrEq{@middleinitial}{}%
{@firstinitial.~@lastname}%
{@firstinitial.~@middleinitial.~@lastname}
}

% Replace full name with bold
newcommand{makeboldname}[1]{%
expandarg
IfSubStr{#1}{name}%
{StrSubstitute{#1}{name}{textbf{name}}par}%
{#1}
}

% Replace last name with bold
newcommand{makeboldlastname}[1]{%
lastname
expandarg
IfSubStr{#1}{@lastname}%
{StrSubstitute{#1}{@lastname}{textbf{@lastname}}par}%
{#1}
}

% Replace surname-first and surname-last with bold
newcommand{makebold}[1]{%
expandarg
IfSubStr{#1}{surnamefirst}%
{StrSubstitute{#1}{surnamefirst}{textbf{surnamefirst}}par}%
{IfSubStr{#1}{surnamelast}%
{StrSubstitute{#1}{surnamelast}{textbf{surnamelast}}par}%
{#1}%
}
}

makeatother

begin{document}

name

surnamefirst

surnamelast

makeboldname{His name was John Doe.}

makeboldlastname{Again, Doe was his last name.}

makebold{I head that Doe, J. was from Kentucky.}

makebold{There was also a J. Doe from New York.}

end{document}


Thank you!










share|improve this question



























    0















    I would like to replace occurrences of strings conditionally within larger strings, using the IfSubStr and StrSubstitute macros from the xstring package. The issue that I'm running into is related to expansion of the makebold macro, which is a function of surnamefirst and surnamelast macro expansion. How can I get the makebold macro to work correctly?



    documentclass{article}

    usepackage{xstring}

    newcommand{name}{John Doe}

    makeatletter

    % Extract last name from name
    newcommand{lastname}{%
    IfSubStr{name}{.}%
    {StrBehind{name}{. }}%
    {StrBehind{name}{ }}%
    [@lastname]
    }

    % Extract first name from name
    newcommand{firstname}{%
    StrBefore{name}{ }[@firstname]
    }

    % Extract first initial from name
    newcommand{firstinitial}{%
    StrLeft{name}{1}[@firstinitial]
    }

    % Extract middle initial from name
    newcommand{middleinitial}{%
    StrBetween{name}{ }{.}[@middleinitial]
    }

    newcommand{testxstring}{%
    middleinitial % Find middle initial
    IfStrEq{@middleinitial}{}%
    {No middle initial!}%
    {Middle initial: @middleinitial}
    }

    % Surname first
    newcommand{surnamefirst}{%
    firstinitial
    middleinitial % Find middle initial
    lastname
    IfStrEq{@middleinitial}{}%
    {@lastname, @firstinitial.}%
    {@lastname, @firstinitial.~@middleinitial.}
    }

    % Surname last
    newcommand{surnamelast}{%
    firstinitial
    middleinitial % Find middle initial
    lastname
    IfStrEq{@middleinitial}{}%
    {@firstinitial.~@lastname}%
    {@firstinitial.~@middleinitial.~@lastname}
    }

    % Replace full name with bold
    newcommand{makeboldname}[1]{%
    expandarg
    IfSubStr{#1}{name}%
    {StrSubstitute{#1}{name}{textbf{name}}par}%
    {#1}
    }

    % Replace last name with bold
    newcommand{makeboldlastname}[1]{%
    lastname
    expandarg
    IfSubStr{#1}{@lastname}%
    {StrSubstitute{#1}{@lastname}{textbf{@lastname}}par}%
    {#1}
    }

    % Replace surname-first and surname-last with bold
    newcommand{makebold}[1]{%
    expandarg
    IfSubStr{#1}{surnamefirst}%
    {StrSubstitute{#1}{surnamefirst}{textbf{surnamefirst}}par}%
    {IfSubStr{#1}{surnamelast}%
    {StrSubstitute{#1}{surnamelast}{textbf{surnamelast}}par}%
    {#1}%
    }
    }

    makeatother

    begin{document}

    name

    surnamefirst

    surnamelast

    makeboldname{His name was John Doe.}

    makeboldlastname{Again, Doe was his last name.}

    makebold{I head that Doe, J. was from Kentucky.}

    makebold{There was also a J. Doe from New York.}

    end{document}


    Thank you!










    share|improve this question

























      0












      0








      0








      I would like to replace occurrences of strings conditionally within larger strings, using the IfSubStr and StrSubstitute macros from the xstring package. The issue that I'm running into is related to expansion of the makebold macro, which is a function of surnamefirst and surnamelast macro expansion. How can I get the makebold macro to work correctly?



      documentclass{article}

      usepackage{xstring}

      newcommand{name}{John Doe}

      makeatletter

      % Extract last name from name
      newcommand{lastname}{%
      IfSubStr{name}{.}%
      {StrBehind{name}{. }}%
      {StrBehind{name}{ }}%
      [@lastname]
      }

      % Extract first name from name
      newcommand{firstname}{%
      StrBefore{name}{ }[@firstname]
      }

      % Extract first initial from name
      newcommand{firstinitial}{%
      StrLeft{name}{1}[@firstinitial]
      }

      % Extract middle initial from name
      newcommand{middleinitial}{%
      StrBetween{name}{ }{.}[@middleinitial]
      }

      newcommand{testxstring}{%
      middleinitial % Find middle initial
      IfStrEq{@middleinitial}{}%
      {No middle initial!}%
      {Middle initial: @middleinitial}
      }

      % Surname first
      newcommand{surnamefirst}{%
      firstinitial
      middleinitial % Find middle initial
      lastname
      IfStrEq{@middleinitial}{}%
      {@lastname, @firstinitial.}%
      {@lastname, @firstinitial.~@middleinitial.}
      }

      % Surname last
      newcommand{surnamelast}{%
      firstinitial
      middleinitial % Find middle initial
      lastname
      IfStrEq{@middleinitial}{}%
      {@firstinitial.~@lastname}%
      {@firstinitial.~@middleinitial.~@lastname}
      }

      % Replace full name with bold
      newcommand{makeboldname}[1]{%
      expandarg
      IfSubStr{#1}{name}%
      {StrSubstitute{#1}{name}{textbf{name}}par}%
      {#1}
      }

      % Replace last name with bold
      newcommand{makeboldlastname}[1]{%
      lastname
      expandarg
      IfSubStr{#1}{@lastname}%
      {StrSubstitute{#1}{@lastname}{textbf{@lastname}}par}%
      {#1}
      }

      % Replace surname-first and surname-last with bold
      newcommand{makebold}[1]{%
      expandarg
      IfSubStr{#1}{surnamefirst}%
      {StrSubstitute{#1}{surnamefirst}{textbf{surnamefirst}}par}%
      {IfSubStr{#1}{surnamelast}%
      {StrSubstitute{#1}{surnamelast}{textbf{surnamelast}}par}%
      {#1}%
      }
      }

      makeatother

      begin{document}

      name

      surnamefirst

      surnamelast

      makeboldname{His name was John Doe.}

      makeboldlastname{Again, Doe was his last name.}

      makebold{I head that Doe, J. was from Kentucky.}

      makebold{There was also a J. Doe from New York.}

      end{document}


      Thank you!










      share|improve this question














      I would like to replace occurrences of strings conditionally within larger strings, using the IfSubStr and StrSubstitute macros from the xstring package. The issue that I'm running into is related to expansion of the makebold macro, which is a function of surnamefirst and surnamelast macro expansion. How can I get the makebold macro to work correctly?



      documentclass{article}

      usepackage{xstring}

      newcommand{name}{John Doe}

      makeatletter

      % Extract last name from name
      newcommand{lastname}{%
      IfSubStr{name}{.}%
      {StrBehind{name}{. }}%
      {StrBehind{name}{ }}%
      [@lastname]
      }

      % Extract first name from name
      newcommand{firstname}{%
      StrBefore{name}{ }[@firstname]
      }

      % Extract first initial from name
      newcommand{firstinitial}{%
      StrLeft{name}{1}[@firstinitial]
      }

      % Extract middle initial from name
      newcommand{middleinitial}{%
      StrBetween{name}{ }{.}[@middleinitial]
      }

      newcommand{testxstring}{%
      middleinitial % Find middle initial
      IfStrEq{@middleinitial}{}%
      {No middle initial!}%
      {Middle initial: @middleinitial}
      }

      % Surname first
      newcommand{surnamefirst}{%
      firstinitial
      middleinitial % Find middle initial
      lastname
      IfStrEq{@middleinitial}{}%
      {@lastname, @firstinitial.}%
      {@lastname, @firstinitial.~@middleinitial.}
      }

      % Surname last
      newcommand{surnamelast}{%
      firstinitial
      middleinitial % Find middle initial
      lastname
      IfStrEq{@middleinitial}{}%
      {@firstinitial.~@lastname}%
      {@firstinitial.~@middleinitial.~@lastname}
      }

      % Replace full name with bold
      newcommand{makeboldname}[1]{%
      expandarg
      IfSubStr{#1}{name}%
      {StrSubstitute{#1}{name}{textbf{name}}par}%
      {#1}
      }

      % Replace last name with bold
      newcommand{makeboldlastname}[1]{%
      lastname
      expandarg
      IfSubStr{#1}{@lastname}%
      {StrSubstitute{#1}{@lastname}{textbf{@lastname}}par}%
      {#1}
      }

      % Replace surname-first and surname-last with bold
      newcommand{makebold}[1]{%
      expandarg
      IfSubStr{#1}{surnamefirst}%
      {StrSubstitute{#1}{surnamefirst}{textbf{surnamefirst}}par}%
      {IfSubStr{#1}{surnamelast}%
      {StrSubstitute{#1}{surnamelast}{textbf{surnamelast}}par}%
      {#1}%
      }
      }

      makeatother

      begin{document}

      name

      surnamefirst

      surnamelast

      makeboldname{His name was John Doe.}

      makeboldlastname{Again, Doe was his last name.}

      makebold{I head that Doe, J. was from Kentucky.}

      makebold{There was also a J. Doe from New York.}

      end{document}


      Thank you!







      xstring






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked 1 hour ago









      Adam EricksonAdam Erickson

      1658




      1658






















          0






          active

          oldest

          votes











          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
          });


          }
          });














          draft saved

          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2ftex.stackexchange.com%2fquestions%2f470467%2fuse-xstring-to-substitute-reformatted-names-in-strings%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          0






          active

          oldest

          votes








          0






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes
















          draft saved

          draft discarded




















































          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.




          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2ftex.stackexchange.com%2fquestions%2f470467%2fuse-xstring-to-substitute-reformatted-names-in-strings%23new-answer', 'question_page');
          }
          );

          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







          Popular posts from this blog

          Futebolista

          Lallio

          Jornalista