Text FormattingJava class substring macro

Information and discussion about LaTeX's general text formatting features (e.g. bold, italic, enumerations, ...)
Post Reply
Kornel
Posts: 3
Joined: Thu Apr 22, 2010 12:42 pm

Java class substring macro

Post by Kornel »

Hi!

I'm trying to create a command which would create a hyperlink based on a fully qualified java class name (e.g. "my.test.package_1.MyTest_Class")

What I'd like to achieve is to create based on e.g. my.test.package_1.MyTest_Class a \href which would point to a url based on this fully qualified class name and as the link name I'd like to have the class name only.

e.g.
from \mysrc{my.test.package_1.MyTest_Class} I want to have:

Code: Select all

\href
	{https://www.xxx.yyy.com/src/my/test/package/MyTestClass.java}%
	{\lstinline{MyTest_Class}}%
}%
This is what I came up with:

Code: Select all

\newcounter {len}
%GETS THE CLASSNAME - i.e. the text after the last dot
\newcommand{\getclassname}[1]{%
    \StrCount{#1}{.}[\dotcount]%
    \StrPosition[\dotcount]{#1}{.}[\lastdotpos]%
	\StrLen{#1}[\arglen]%
	\setcounter{len}{0}%
    \addtocounter{len}{\arglen}%
    \addtocounter{len}{-\lastdotpos}%
\StrRight{#1}{\value{len}}%
}%

%CREATES THE LINK
\newcommand{\mysrc}[1]{%
\StrSubstitute{#1}{.}{/}[\SLASHNAME]%
\getclassname{#1}[\CLASSNAME]%
\href% 
	{https://aaa/src/\SLASHNAME.java}%
	{\CLASSNAME}%
}%
The problem is with the class name. As it might contain underscores, I thought I could wrap it in a \verb or \lstinline but that seems to be illegal.

Do you have any ideas, how I could achieve this task and maybe improve a bit my command (I'm quite new at writing latex commands)?

Thanks,
Kornel

Recommended reading 2024:

LaTeXguide.org • LaTeX-Cookbook.net • TikZ.org
LaTeX Beginner's Guide LaTeX Cookbook LaTeX TikZ graphics TikZによるLaTeXグラフィックス
Post Reply